PyPNG and the Gimp
Up until now I’ve been using the Python Imaging Library (PIL) to save my strange attractor images. Unfortunately PIL’s grey-scale mode seems to be 8-bit only. That’s only 256 levels of grey. While that is probably fine for a finished image it really doesn’t leave much wriggle room if I want to adjust levels and colors later.
PyPNG, while not a full featured imaging library, specialises in reading and writing png images and does support, among other things, 16-bit pixel depth.
The procedure for converting an array of integers to an image differs slightly in these two packages as I will briefly demonstrate.
Suppose you have an array of integers named imgArray. Note that PIL requires a special 8-bit integer as input. If you try to use vanilla Python integers the resulting image is all wrong.
# convert array to 8-bit integers imgarray = np.array(imgArray, np.uint8) # create the image object # "L" is the greyscale mode (luminance) img = Image.fromarray(imgarray, "L") # saving is easy img.save("myFileName.png", "PNG")
# PyPNG works with "Writer" objects rather than "Image" objects imgWriter = png.Writer(WIDTH, HEIGHT, greyscale=True, alpha=False, bitdepth=16) # A file must be opened for binary writing ("wb") explicitly f = open("myFileName.png", "wb") # ImgWriter expects a list of lists (rows and columns) as input, # but a numpy array works just as well. # RGB images (a 3D array) require array the to be reshaped into a 2D array first. imgWriter.write(f, imgarray) # remember to close the file! f.close()
I’ve also made some preliminary attempts at colorizing these patterns in GIMP. I’m quite handy with Photoshop, but I’ve never really used GIMP for more than some really minor work. I definitely want to become more proficient with this tool.