6

I am trying to have a QLabel() display a pixmap JPG image from a file (which can't be in a resource file since it is downloaded from the web), but I am having problems loading it. The code is fairly simple:

label = QLabel()
label.setPixmap(QPixmap("image.jpg"))

It works with PNG files, but it doesn't work with JPG files. I have Googled quite a bit and found that I needed to place the "imageformats" folder in the same folder where my script is located. However, even after doing this (and yes, qjpeg4.dll and others are there), it still doesn't work. I've also tried doing

path = r"C:\Users\Deusdies\Documents\Work\myapp\imageformats"
app.addLibraryPath(path)

but that didn't help either.

Again, it loads PNGs just fine, but it won't load JPGs. I've also noticed even before that it won't load ICO either, but I thought of it as an unrelated issue - however it doesn't seem that way now.

It is worth noting that the application is not converted to an .exe at this point - it is ran through python.exe interpreter via PowerShell.

My development environment is Windows 7 x64, PySide 1.1.0

How can I solve this problem?

NorthCat
  • 9,643
  • 16
  • 47
  • 50
Bo Milanovich
  • 7,995
  • 9
  • 44
  • 61
  • What Qt version? To see available image formats: `list = QImageReader.supportedImageFormats ()` then `print()` this list. – dschulz May 24 '12 at 16:54
  • Are you completely sure that image is not in another format, with just the `.jpg` extension? – dschulz May 24 '12 at 16:57
  • @dschulz: I got this: [PySide.QtCore.QByteArray('bmp'), PySide.QtCore.QByteArray('pbm'), PySide.QtCore.QByteArray('pgm'), PySide.QtCore.QByteArray('png'), PySide.QtCore.QByteArray('ppm'), PySide.QtCore.QByteArray('xbm'), PySide.QtCore.QByteArray('xpm')] Obviously the JPG is missing. Any idea how to add it? Also yes I am 100% sure it's a JPG. – Bo Milanovich May 24 '12 at 18:15
  • I vaguely remember a blog post from a Qt developer about one specific image format plugin which was causing this kind of problems. But I think it was fixed long ago. Is your software updated? – dschulz May 24 '12 at 18:24
  • Here's the blog post I'm talking about: [Qt Image Decoders Stepping on Each Others](http://agateau.com/2012/03/30/qt-image-decoders-stepping-on-each-others/) – dschulz May 24 '12 at 18:25
  • Yes it's updated, using the latest stable Qt. The blog post talks about the inability to load PNGs though. – Bo Milanovich May 24 '12 at 18:29

1 Answers1

5

I solved the problem. First, path should look like this:

path = r"C:\Users\Deusdies\Documents\Work\myapp"

(so without the "imageformats" part)

And second, I was an idiot. I created an instance of the QDialog() class before doing the addLibraryPath()

Bo Milanovich
  • 7,995
  • 9
  • 44
  • 61
  • now there's another problem pending.. Why it worked with PNG images?? ;-) – dschulz May 25 '12 at 00:54
  • 2
    That's because PNG support has no corresponding plugin in imageformats/, it's either included or not depending on the more general Qt configuration. – Daniel Vérité May 25 '12 at 08:35
  • @Deusdies: is there documentation on this issue at the pyside site? I couldn't find it. – eric Jun 08 '14 at 17:31