[Image-SIG] PIL - Opens tga as WbmpImageFile

Zac Burns zac256 at gmail.com
Tue May 19 02:33:03 CEST 2009

On Mon, May 18, 2009 at 4:11 PM, Fredrik Lundh <fredrik at pythonware.com> wrote:
> On Mon, May 18, 2009 at 8:05 PM, Zac Burns <zac256 at gmail.com> wrote:
>> On Mon, May 18, 2009 at 9:49 AM, Zac Burns <zac256 at gmail.com> wrote:
>>> I have a script that is attempting to verify that an image is square.
>>> This is accomplished using PIL.Image.open(filename).size
>>> Within the context that the script is being run it is opening a tga as
>>> a WbmpImageFile and reporting the size to be (2, 0).
>>> However, printing repr(filename), copying the filename, and running
>>> the line with that file from a fresh Python interpreter gives the
>>> correct results.
>>> PIL version is 1.1.6
>>> Python version is 2.5.1
>>> --
>>> Zachary Burns
>>> (407)590-4814
>>> Aim - Zac256FL
>>> Production Engineer (Digital Overlord)
>>> Zindagi Games
>> I locally patched the PIL.Image.open function to fix the bug and made
>> some other improvements along the way.
>> I'm not familiar with the PIL patch process and am not sure that the
>> changes will be accepted by the community so I'll just post the code
>> here.
>> Chages:
>>   1. More informative error message
>>   2. Optimization: Moved import __builtin__ to top of file. (I'm not
>> sure why but importing is relatively slow on my machine)
>>   3. Factored shared code into an inline function
>>   4. Does preinit and init before trying any plugins
>>   5. Prefers the extension of the filepath
> This will change the open semantic enough to risk breaking tons of
> stuff, and also slow PIL down a lot for people using only common file
> formats (and if importing the builtin module is slow on your machine,
> something's fishy with your interpreter - that module is built into
> the Python core).
> The right way to fix this would be to make the TGA accept function
> smarter to avoid false positives.  Do you have any samples of the
> problematic WBMP files that you can share? (feel free to mail them
> directly to me; I won't make them public without explicit permission)
> </F>

Please explain the case that it would be slower. I'm not sure I
follow. It seems to me that the change would for many people actually
make loading images faster because rather than try each accept
function in succession (in some cases twice over) we take a 'best
guess' approach first, then go to the other functions (once over). The
worst case appears to be one extra check. Best case we go straight for
the correct loader.

About the example, I think it's reversed - WBMP is the one giving the
false positive because it opens the .tga file, which is using targa
compression, as a WBMP. If I have time after work I'll try and figure
out a little more about what is causing this before sending an example
because like I said in the original e-mail it's something about the
context which is causing this - if I simply run the code in a fresh
interpreter it opens as a tga just fine.

About the importing of __builtin__, it's a very small speed
improvement - only a couple of milliseconds. Which, is slow enough
that I've started putting all my imports at the top of all files as a
habit - even if it's not a huge difference in this case. This is also
part of the python style guide.

Zachary Burns
Aim - Zac256FL
Production Engineer (Digital Overlord)
Zindagi Games

More information about the Image-SIG mailing list