[Image-SIG] Simple ICO => PNG conversion problem

Karsten Hiddemann karsten.hiddemann at mathematik.uni-dortmund.de
Thu May 1 18:36:22 CEST 2008


The IcoImagePlugin is among the oldest code in PIL. Have a look at it, 
all it does is finding out where the InfoHeader for the biggest icon 
starts in the file (since an icon file can contain more than one image), 
calls the BMP plugin with that, and then adjusts the image height. This 
closely matches the structure in http://www.daubnet.com/formats/ICO.html 
which is for pre-WinXP icon formats, I think. PIL plain ignores the 
transparency which comes from the AND mask inherent to the ICO format.

The IcoImagePlugin should be consequently rewritten from scratch to 
support things like the ContainerIO, if somebody cares. If you look at 
http://www.blitzbasic.com/Community/posts.php?topic=75770 or the 
Wikipedia article for the ICO file format you'll see that XP as well as 
Vista extended the file format.

But unless somebody does this, here's a way for you to get the 
transparency right when loading your icon:

	i = Image.open("favicon.ico")
	d, e, o, a = i.tile[0]
	s = file("favicon.ico").read()
	mask = Image.fromstring('1', i.size, s[o+e[2]*e[3]:], "raw", ('1;I', 4, 
-1))
	i.putalpha(mask)
	i.save("test.png")

Cheers,
Karsten


More information about the Image-SIG mailing list