Home > Graphics, Python, Strange Attractors > PyPNG and the Gimp

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.

With PIL:

# 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")

With PyPNG:

# 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!

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.

Purple Haze

  1. December 28, 2009 at 9:29 am

    I’m glad you like PyPNG’s 16-bit support and the “magic numpy” feature.

    I am struck by how much easier PIL’s interface is to use, it’s much less code. I’ll try and make PyPNG as easy to use as that.

    • December 28, 2009 at 6:25 pm

      Hi, thanks for your work developing PyPNG! I really didn’t think that it was such a lot of code to save an image, but now that you mention it I suppose it could be simplified. Opening and closing a binary file really just seems to be boilerplate so it would be nice if this was wrapped up in the PyPNG module somehow.

  2. December 28, 2009 at 9:39 am

    PS. I create Issue 57, http://code.google.com/p/pypng/issues/detail?id=57

    And I like your attractors. You may wish to investigate the gAMA chunk.

    • December 28, 2009 at 6:30 pm

      Thanks. I really must train myself to be more active in the projects that I use, such as PyPNG.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: