7

I'm trying to get QImage (or anything in PyQt4, for that matter) to load a jpg file in a script. I haven't been able to find any information on how to get it to load a jpg image as a script, lots for compiled using py2exe but I can't even get that far.

Spending some time to resolve this, I've followed a few things with no available. Looking in my site-packages/PyQt4/plugins/imageformats folder I have:

qgif4.dll, qico4.dll,qjpeg4.dll, qmng4.dll, qsvg4.dll, qtga4.dll, qtiff4.dll

According to QtGui.QImageReader.supportedImageFormats(), this is what my install of pyqt4 can use

[PyQt4.QtCore.QByteArray('bmp'), PyQt4.QtCore.QByteArray('pbm'), PyQt4.QtCore.QByteArray('pgm'), PyQt4.QtCore.QByteArray('png'), PyQt4.QtCore.QByteArray('ppm'), PyQt4.QtCore.QByteArray('xbm'), PyQt4.QtCore.QByteArray('xpm')]

I've also made sure that my qt.conf file is in the main python directory and it has this

[Paths]

Prefix = C:/Python27/Lib/site-packages/PyQt4
Binaries = C:/Python27/Lib/site-packages/PyQt4

I've tried adding

Plugins = C:/Python27/Lib/site-packages/PyQt4/plugins/imageformats
Plugins = C:/Python27/Lib/site-packages/PyQt4/plugins

with no luck

I'm running python 2.7.2, with PyQt4 4.9.1(both 32b), on a windows 7 64b Home Premium.

I've also tried version 4.8.1 and 4.8.5 (which I had on hand) but none of them supported jpg either. I'm at a loss. How do I get these back to being supported?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
ooklah
  • 491
  • 1
  • 5
  • 16
  • Strange. For me this had been a concern once on OSX when it came to packaging with py2app, but never in a script situation. It always managed to find the plugin properly. – jdi May 07 '12 at 05:30
  • Wow, it's not just `JPEG` It seems PyQt can't load any of those `dll` files. Can you try `Plugins = plugins`? It shouldn't be full path. – Avaris May 07 '12 at 06:04
  • I gave plugins a try, and then tried '/plugins', '/plugins/imageformats', 'plugins/imageformats' for good measure. nothing. – ooklah May 07 '12 at 07:13
  • Have you tried simply wiping your python and PyQt installs completely and reinstalling? Presumably this works for most people, so unless you're doing something weird in your code, it's possible your install is just mucked up. – Cryptite May 08 '12 at 15:35
  • I downloaded the latest python 2.7.3 msi and PyQt4 4.9.1 exe installers from their respective websites and installed them onto a new machine that hadn't gotten them installed before. The new machine also cannot load the extra image formats – ooklah May 08 '12 at 16:31

2 Answers2

8

There are two different issues it seems that can contribute to causing this. And it looks like I ran into both.

1st. I gave running the PyQt4 installer 'as administrator' which seems to have solved part of the issue. So it needs to be run as administrator to get everything to work correctly.

2nd. In order for all the plugins to load properly, the QApplication must be made first before any kind of image is loaded.

so

app = QtGui.QApplication(sys.argv)

needs to be created first.

My scripts run as

def main():
    app = QtGui.QApplication(sys.argv)
    win = window()
    win.display()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

which hasn't changed since I installed PyQt4 as an administrator but now works and loads jpegs and everything else.

What I was trying to do, was create a script that was going to be called by a PyQt4 application but it wouldn't be run by itself, so there was no need to create a QApplcation first. Which is where I ran into issue #2

For reference: http://article.gmane.org/gmane.comp.python.pyqt-pykde/7176/match=jpeg+plugin+not+loading

Install as administrator and always create the QApplication, even if you don't need it.

also if you are just checking to see what's available in idle:

from PyQt4 import QtGui
QtGui.QImageReader.supportedImageFormats()

won't show everything, still need to run

from PyQt4 import QtGui
import sys
app = QtGui.QApplication(sys.argv)
QtGui.QImageReader.supportedImageFormats()

This was annoying to track down, so hopefully this will be helpful to others.

ooklah
  • 491
  • 1
  • 5
  • 16
  • 1
    So, I just ran into this with virtualenvs on Windows (copied PyQt4 from global site-packages to envs site-packages) - I had to create the qt.conf like specified in the original post next to Scripts\python.exe - then everything worked like it should! – Strayer Jan 16 '13 at 21:28
  • If anyone knows how to load the plugins without creating a QApplication, please add a comment here; it would be very helpful for those of us just using QImage as a utility image-loading class. – GaryO Apr 18 '14 at 17:02
  • Mine wouldn't work until I put the QApplication call in __main__. It did not work with QApplication in main() as you showed it. Very weird, but eliminating use of main() fixed it for me. – panofish Mar 05 '15 at 20:54
0

Just in case someone is searching for loading plugins, I've created an answer for a more proper question here to allow you basically put plugins in any network shared folder.

In short, use QtCore.QCoreApplication.addLibraryPath() instead of qt.conf file.

Community
  • 1
  • 1
PenguinTD
  • 319
  • 3
  • 4