1

I'm trying to write a program that creates a png file and paints the outer pixel black. I only need black and white so bitdepth=1 works for my situation.

import png
import numpy as np 

MazeHeight = 5
MazeWidth = 7

if __name__ == "__main__":
    file = open('png.png', 'wb')
    writer = png.Writer(MazeWidth, MazeHeight, greyscale=True, bitdepth=1)
    #generating white array
    Maze = np.ones((MazeHeight,MazeWidth),dtype=int)
    #mark borders as black
    for i in range(MazeHeight):
        for j in range(MazeWidth):
            #top/bottom bordes
            if i == 0 or i == MazeHeight-1:
                Maze[i][j] = 0
            #left/right
            elif j == 0 or j == MazeWidth-1:
                Maze[i][j] = 0
    writer.write(file, Maze)
    file.close()

If I print the Maze to the console it looks fine:

[[0 0 0 0 0 0 0]
 [0 1 1 1 1 1 0]
 [0 1 1 1 1 1 0]
 [0 1 1 1 1 1 0]
 [0 0 0 0 0 0 0]]

The png.png file does not look like the numpy array

[[1 1 1 1 1 1 1]
 [1 1 1 1 1 1 1]
 [1 1 1 1 1 1 1]
 [0 1 1 1 0 1 1]
 [1 1 1 1 1 1 1]]

(1 is black, 0 is white since I cant upload pictures)

I dont know why my console output is different from the png-file. I'm struggling to read the png-file. I know there is a read() method with a png.Reader but throws an Error: "png.FormatError: FormatError: PNG file has invalid signature."

Wave
  • 21
  • 2
  • You can upload a picture in SO. https://meta.stackexchange.com/questions/75491/how-to-upload-an-image-to-a-post – Ehsan Aug 26 '20 at 01:54
  • If i change the input from array to range(int) i get the expected output. But i cant paint a border only using the range(int) function. I assume the png class handles the return value from range different than an array list. `writer = png.Writer(MazeWidth, MazeHeigth, greyscale=true)` `writer.write(file, range(255))` – Wave Aug 26 '20 at 09:04

1 Answers1

1

Found my problem myself: instead of int i have to use unsigned bytes for the Image. Maze = np.ones((MazeHeight,MazeWidth),dtype=int) to Maze = np.ones((MazeHeight,MazeWidth),dtype=uint8)

Wave
  • 21
  • 2