Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QLabel() won't load a pixmap if it is a JPG image

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?

like image 854
Bo Milanovich Avatar asked May 24 '12 14:05

Bo Milanovich


People also ask

How to display an image in PyQT?

A QPixmap can be used to show an image in a PyQT window. QPixmap() can load an image, as parameter it has the filename. To show the image, add the QPixmap to a QLabel. QPixmap supports all the major image formats: BMP,GIF,JPG,JPEG,PNG,PBM,PGM,PPM,XBM and XPM.

How to display a picture in PyQt5?

From the property editor dropdown select "Choose File…" and select an image file to insert. As you can see, the image is inserted, but the image is kept at its original size, cropped to the boundaries of the QLabel box. You need to resize the QLabel to be able to see the entire image.


1 Answers

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()

like image 186
Bo Milanovich Avatar answered Oct 22 '22 01:10

Bo Milanovich