Skip to content Skip to sidebar Skip to footer

Pyqt4 Jpeg/jpg Unsupported Image Format

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

Solution 1:

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:

fromPyQt4importQtGuiQtGui.QImageReader.supportedImageFormats()

won't show everything, still need to run

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

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

Solution 2:

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.

Post a Comment for "Pyqt4 Jpeg/jpg Unsupported Image Format"