Python: 由于文件签名前的文本而无法打开的 .png 图像文件

Python: .png image file that wont open because of text before file signature

我有大量的 png 文件,在 ‰PNG 文件签名之前添加了一些文本。我正在尝试找到一种方法来打开 python 中的文件并在写入新文件之前删除多余的文本,以便我可以查看图像。

可以在下图中看到文件的 Notepad++ 屏幕截图,以便更好地理解问题。

first few lines of file showing additional text before file signature

到目前为止我已经尝试过这段代码

infile = open('radar0.1.107652907', encoding='ANSI')
outfile = open('test.png', 'w', encoding='ANSI')

imagetext = infile.read()

pos = imagetext.find('‰')

outtext = imagetext[pos:]

outfile.write(imagetext)

但是当我尝试打开新文件时,它打不开。

任何帮助将不胜感激

我能够使用@BoarGules 建议以二进制方式解决问题。代码如下

infile = open('radar0.1.107652907', 'rb')
outfile = open('test.png', 'wb')

imagetext = infile.read()

pos = imagetext.find(b'\x89PNG')

outtext = imagetext[pos:]

outfile.write(outtext)


infile.close()
outfile.close()