问题描述
我正在尝试制作一个小的python程序,以不同的方式组合图像,但是当我尝试打开图像时会抛出该图像:
png.FormatError: FormatError: PNG file has invalid signature.
这是代码:
from png import Reader,Writer
from sys import argv
from io import StringIO
fileName = argv[1];
directions = ["left","right","top","bottom"]
readers = {
"left": Reader(StringIO(
"\"folder"+fileName+"_left.png\""
)),"right": Reader(StringIO(
"\"folder"+fileName+"_right.png\""
)),"top": Reader(StringIO(
"\"folder"+fileName+"_top.png\""
)),"bottom": Reader(StringIO(
"\"folder"+fileName+"_bottom.png\""
))
}
images = {
"left": list(),"right": list(),"top": list(),"bottom": list()
}
for direction in directions:
reader = readers[direction]
image = images[direction]
tempImage = reader.asRGBA8() # error
我确定文件没有损坏,因为它在所有内容(aseprite,gimp,paint,paint.net和krita)中都可以正常打开,还有什么可能导致此错误?
解决方法
遇到同样的错误。现在这段代码对我有用:
# -*- coding: utf-8 -*- import png r = png.Reader(file=open("./image.png","rb") ) r.read()
成功读取图像。在 python 2.7 和 3.7 版本中测试。打开时注意“rb”模式。