Tuesday, February 11, 2014

Pygame attacks - "File is not a Windows BMP file"

There are many users with problem regarding loading pygame images, be it jpeg or png.

"File is not a Windows BMP file" - not really helpful in this case :)

As per pygame documentation, to check if extended image support was enabled you should do:

>>> print(pygame.image.get_extended())
0

Zero means it isn't, but in my case I knew it was enabled and if you don’t - it probably is.

Road to enlightenment

I’ve opened my Python3.3 installation site-packages dir (your path may vary): /usr/lib/python3.3/site-packages/pygame/

there was image.so file which is a pygame.image module inside python interpreter, but what about other similar file - imageext.so? Both use libpng and image.so imports successfully.

So what happens if I try to import imageext?

Well, that happens:

>>> import pygame.imageext
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: libpng15.so.15: cannot open shared object file: No such file or directory

Now, that is a core of this problem - libpng version has changed and pygame depends on older one.

How to fix

There are 3 ways i can think of:

Quick one - You could symlink expected patch to current libpng but its ugly way and may yield unpredictable results ™.

Somewhat good - You can downgrade system libpng version to match expected

Best one - Fix everything! Recompile things if your system allows it - I’m using Gentoo so it is the way I choose ;)

So, for the best way:

On any distribution - to list libs depending on missing file run:

grep -R libpng15.so.15 /usr/lib/*

Command could yield something like that:

Plik binarny /usr/lib/libSDL_image-1.2.so.0 pasuje do wzorca
Plik binarny /usr/lib/libSDL_image-1.2.so.0.8.4 pasuje do wzorca
Plik binarny /usr/lib/libSDL_image.so pasuje do wzorca

The sdl-image package got broken, and pygame depends on it.

Now, we have to just reinstall/update sdl-image package and everything should work. To the next libpng update :)