List Info

Thread: Fixes to PIL for handling XPM files.




Fixes to PIL for handling XPM files.
user name
2006-10-24 15:07:24

I recently downloaded the Python Imaging Library (PIL 1.1.5), and I have been trying to use it to convert several hundred XPM files into GIFs (I also tried converting into PNGs which may be useful in the future).  During this process, I ran into various problems which appear to be caused by bugs.

Most of the difficulties were related to XPMs -- not being able to load XPM files which other applications were quite happy with. ; But there were also a couple of minor issues with transparency in GIF and PNG formats.

Attached is a diff file containing the changes I had to make to the code to fix these problems.  This fixes things for the vast majority of the XPM files I have. ; There are still some XPMs that can't be loaded (those which use more than one character per pixel for example), but these are far less common.


Because I am not a regular contributer to this project I thought I should include more information than just the diffs, so here is a list of the bugs I found and what I changed as a result:



Bug: PIL could not open some XPM files because it failed to cope with some features of XPM syntax.

Fix: ; Various changes in XpmImagePlugin.py:

  • Changed the RE used to match the image header to one that copes with leading spaces and multiple spaces between words.
  • In XpmImageFile._open(), when reading the image palette:
  • Ignore lines which are commented-out.
  • Strip leading and trailing whitespace before processing.
  • Remove case sensitivity when looking for "None".
  • Use ImageColor.getrgb() to interpret color values.
  • Look ahead to detect colour names consisting of multiple words
  • In XpmImageFile.load_read() (when reading the pixels) ignore lines which are commented-out.




Bug: PIL could not open some XPM files, because the color names were not recognized.

Fix: various changes to ImageColor.py:

  • Changed the colormap to the standard list of X11 colours (as found on any Unix or Linux system).
Note: before this change, the colormap apparently contained colours taken from CSS3 -- the comment reads 'X11 colour table (from "CSS3 module: Color working draft")'.  However some of the standard X11 colours clashed with existing colours in the map.  In the end, I removed all the CSS3 colours and just included the X11 colours.  The comment does say "X11 colour table" after all.
  • getrgb(): Remove spaces from the color name in order to find a match.  X11 has variations of some color names, such as "light blue" and "LightBlue".   But the version with spaces was not recognized.
  • getrgb(): Recognize colours specified as a 48-bit RGB string (e.g. #FFFF0000AAAA).  These are sometimes encountered in XPMs. ; The 3 bytes of extra precision are ignored, and the returned RGB value is a standard 24-bit RGB tuple.



Bug: PIL loaded all XPM files incorrectly, shifting the entire image several pixels to the right (truncating it on the right, and adding a a band of incorrect pixels on the left).

This is caused by trying to use memory mapping to read the file, which prevents the proper XPM decoder from being used. ; Memory mapping does not work because of the structure of XPM files -- the file includes characters (such as the opening and closing quotes on each line) which do not correspond to pixel values.

Fix: Changed load() in ImageFile.py to explicitly exclude XPM files from the memory mapping.

There may be a more elegent way to do this, perhaps by changing the value of ImageFile.tile for XPMs. ; But I tried the obvious thing -- changing the "raw" designation to something else, and that seemed to break things even more. ;  Perhaps someone more familiar with this code could do better.



Bug: Could not write GIFs with transparency.  The pixels which should have been transparent were not.

Fix: Changed _save() in GifImagePlugin.py.   The code was looking in the wrong dictionary for the transparency value.



Bug:
Could not write PNGs with transparency.  The pixels which should have been transparent were not.

Fix: Changed _save() in PngImagePlugin.py.  The code was looking in the wrong dictionary for the transparency value.


--
Tom Heathcote

-- 
Tom Heathcote              Petris Technology
petris.com">tom.heathcotepetris.com   154 Brent Street
Tel +44 20 8202 2433       London  NW4 2DR
Fax +44 20 8202 2287       England
Fixes to PIL for handling XPM files.
user name
2006-10-26 08:23:39
Tom Heathcote wrote:

> Attached is a diff file containing the changes I had to
make to the
 > code to fix these problems.  This fixes things for the
vast majority
 > of the XPM files I have.

thanks.  no time to comment on all these right now, but I
couldn't help 
noticing the following:

> *Fix Changed
_save() in GifImagePlugin.py.   The code was looking in 
> the wrong dictionary for the transparency value.

> *Fix Changed
_save() in PngImagePlugin.py.  The code was looking in 
> the wrong dictionary for the transparency value.

the "info" dictionary is a free-form dictionary
used to return in- 
formation from plugin loaders.  its content is not
standardized,
is dropped by most image operations, and is therefore not
used by "save".

instead, you have to use an explicit option when saving an
image:

     im.save(filename, transparency=value)

(that you had to change all plugins should have told you
that maybe you 
weren't using the API in the intended way, don't you think?


cheers /F

_______________________________________________
Image-SIG maillist  -  Image-SIGpython.org
htt
p://mail.python.org/mailman/listinfo/image-sig
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )