0

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?

lenerdv
  • 177
  • 13

1 Answers1

0

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.

kmsvsr
  • 1
  • Hey, welcome to StackOverFlow. if I need to sumup your answer I would say you suggest using mode `rb`. The user that asked the question wrote a similar code to yours except of specifying the mode. I would edit your answer in a form it would answer the question in a more clear way. – Green Mar 07 '21 at 18:32
  • Ok, I`m not english-native, if i wrote something wrong then you can edit my answer. – kmsvsr Mar 12 '21 at 04:06