motatoes
motatoes

Reputation: 926

Python: PyQt QPixmap returns Null for a valid image

I'm trying to load this .tiff image into my gui interface using PyQt's QPixmap. I have the following code:

fileName = QFileDialog.getOpenFileName(self.parent, "Open Image", "/", "Image Files (*.png *.jpg *.bmp *.tiff)");
img = QtGui.QPixmap(fileName)
scaled_img = img.scaled(self.ui.img_label.size(), QtCore.Qt.KeepAspectRatio)
self.ui.img_label.setPixmap(scaled_img)

Where img_label is a Qlabel in my GUI. I tested it with various .jpg, .png and .tiff images. It seems to be working, but when I test it on this image, it returns Null

What I have tried

Can someone suggest what I might be doing wrong?

Upvotes: 3

Views: 5758

Answers (2)

motatoes
motatoes

Reputation: 926

Okay, this is a bit strange. The image loads when I change the line:

img = QtGui.QPixmap(fileName)

To:

img = QtGui.QPixmap(fileName, "1")

In fact it seems to work when I specify any numeric string for the second argument. I'm not sure the second argument is supposed to be as the documentation is a bit vague about it. I hope someone can explain what's happening, as this is really puzzling me.

Upvotes: 4

Pouya
Pouya

Reputation: 159

QFileDialog.getOpenFileName() returns a tuple. try this instead:

fileName = QFileDialog.getOpenFileName(self.parent, "Open Image", "/", "Image Files (*.png *.jpg *.bmp *.tiff)");
img = QtGui.QPixmap(fileName[0])
scaled_img = img.scaled(self.ui.img_label.size(), QtCore.Qt.KeepAspectRatio)
self.ui.img_label.setPixmap(scaled_img)

Upvotes: 1

Related Questions