Home > Graphics, Python > PIL Revisited

PIL Revisited

I made a mistake a couple of days ago regarding Python Imaging Library’s greyscale modes – I thought at the time that PIL supported 8-bit greyscale only.

The “L” mode is certainly 8 bits deep, but there is another mode, “I”, which is 32 bits deep, and “I;16” is for 16 bit images. Many of the image operations offered by PIL fail to operate on these 16 and 32 bit images however.

Also, PIL won’t save to a 16 bit png file, but it will save to a 32 bit png file so that’s not such a big deal.

Advertisements
Categories: Graphics, Python Tags: , ,
  1. January 3, 2010 at 3:34 am

    PNG doesn’t have a 32-bit greyscale format (but it does have a 16-bit greyscale format). Do you have an example of one of the “32 bit png” images that PIL saves? I would like to examine it for testing.

    • January 3, 2010 at 2:55 pm

      I just found this note from an old PIL change log:

      + Changed the PNG driver to load and save mode “I” images as 16-bit images. When saving, values outside the range 0..65535 are clipped.

      “I” of course is the 32-bit mode, and “I;16” is the 16-bit mode. So saving as 16 bit PNG isn’t allowed, but saving as a 32 bit PNG silently clips to 16 bits? Pretty weird!

  2. January 3, 2010 at 2:44 pm

    Well, I just attempted to create a PNG file using PIL’s 32bit unsigned integer mode, and while it saves without raising an error, the result doesn’t look right. The pixels all display as either black or white. The same image object when saved as a TIFF file looks fine with all of the intermediate shades of grey that I would expect. I guess it isn’t possible after all.

    It’s interesting that PIL raises an error if you try to save a 16-bit greyscale PNG, but not when you try to save a 32-bit greyscale PNG even though it seems not to work properly. A bug I suppose.

  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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: