简体   繁体   English

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

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

I have a large series of png files that have had some text added before the ‰PNG file signature.我有大量的 png 文件,在 ‰PNG 文件签名之前添加了一些文本。 I'm trying to find a way of opening the files in python and deleting the extra text before writing to a new file, so I can view the images.我正在尝试找到一种方法来打开 python 中的文件并在写入新文件之前删除多余的文本,以便我可以查看图像。

A screenshot from Notepad++ of the file can be seen in the below image to give a better understanding of the problem.下图中可以看到来自 Notepad++ 的文件截图,以便更好地理解问题。

first few lines of file showing additional text before file signature文件签名前的前几行显示附加文本

So far I have tried this code到目前为止,我已经尝试过这段代码

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)

But when I try and open the new file it wont open.但是当我尝试打开新文件时,它不会打开。

Any help would be massively appreciated任何帮助将不胜感激

I was able to solve the issue using @BoarGules suggestion to work in binary.我能够使用@BoarGules 建议以二进制方式解决问题。 Code is below代码如下

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM