Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling JPEG support for QImage in py2exe-compiled Python scripts?

I'm trying to use a JPEG image in a QImage object from a Python script, with PyQt4.

The script itself works perfectly, the image loads and can be manipulated and rendered and all. However, when I try to "compile" this script with py2exe, everything works but the JPEG image. Replacing it with a PNG equivalent works, but since my program downloads images from the web, they won't always be in PNG format and I can't afford converting them all with another library.

I've discovered that JPEG image support for QImage, along with other image formats, is provided by some DLLs in the \qt\plugins\imageformats directory (they're called qjpeg4.dll and qjpeg4d.dll). I think I need to use them somehow in my executable, but I don't know how. I've tried simply copying them to my exe directory, no luck. I've tried, as indicated here, to include those files as data_files in the setup.py script, but no luck (it looks like all it does is copying these files to the exe's directory, so it changes nothing from copying them manually anyway).

I'm sure there's a handful of applications out there using PyQt with JPEG images, how do they do it? It seemed like a trivial task but I'm stuck on it now.

Also, I want my app to be cross-platform (why else would I be coding in Python?), I hope I won't run into such packaging trouble (it's not the only one) when packaging for OS X or Linux. Will I?

like image 966
Etienne Perot Avatar asked May 20 '09 02:05

Etienne Perot


4 Answers

After hours of stumbling around with the same issue, I’d like to share the solution that worked for me on windows vista: using python2.6

copy the following directory into your dist directory generated by py2exe:

C:\Python26\Lib\site-packages\PyQt4\plugins\imageformats

I just dropped the imageformats directory directly into my dist directory, without any further modifications to qt.conf or anything like that. I haven’t tried it, but this may work for phonon as well.

like image 135
jbz Avatar answered Oct 18 '22 16:10

jbz


I'll have to confess I never managed to get the py2exe + pyqt combination quite right (and, py2exe doesn't help at all with cross-platform packaging). PyInstaller seems to be much better -- the docs at http://www.pyinstaller.org/ are old, but the svn trunk is much more recent. Some docs are in slides given at the recent Pycon Italia Tre conference -- http://www.pycon.it/static/stuff/slides/distribuire-programmi-python-con-pyinstaller.pdf -- and, the slides are in English, and contain the current maintainer's email, so they should help! (And, let's all lobby the current maintainer to update the docs...!-)

like image 24
Alex Martelli Avatar answered Oct 18 '22 16:10

Alex Martelli


Try adding a qt.conf file to your exe's directory, to tell qt where to find binaries and plugins.

Something like the following works for the simple case, where you just dump all dll's in the same dir as the exe:

[Paths]
Prefix = .
Plugins = .

Update: Then copy your plugins-contents (the imageformat/sqldriver directories etc) to the exe dir. I don't think you can load plugin dlls from the same directory as the exe. See Qt plugin doc for details on plugon subdirectories. (Or, leave out the 'plugins = .' and copy the plugins dir to the exe dir, so you have /plugins/imageloaders/qjpeg4.dll).

like image 36
Macke Avatar answered Oct 18 '22 17:10

Macke


Etienne -- Thank you for the tip. After much reading and trial-and-error, I arrived at the same conclusion: use PIL to show jpegs in a py2app-generated app.

http://www.thetoryparty.com/wp/2009/08/27/pyqt-and-py2app-seriously-i-dont-know-what-to-do-with-you-when-youre-like-this/

What I guess is that the proposed solutions for py2exe/Windows don't necessarily apply to py2app/OSX.

like image 42
AteYourLembas Avatar answered Oct 18 '22 15:10

AteYourLembas