简体   繁体   中英

PyPng throws signature FormatError when PNG is valid

I'm trying to make a small python program that combines images in different ways, but when I try to open an image it throws: png.FormatError: FormatError: PNG file has invalid signature.

Here's the code:

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

I'm sure the file isn't corrupted, as it opens just fine in everything (aseprite, gimp, paint, paint.net and krita), what else could cause this error?

Got the same error. Now this code works for me:

# -*- coding: utf-8 -*-
    import png
    r = png.Reader(file=open("./image.png", "rb") )
    r.read()

Reading images successfuly. Tested in python 2.7 and 3.7 versions. Pay attention to "rb" mode when open.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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