Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  • I have tried printing QImageReader.supportedImageFormats and it shows that .tiff is supported
  • Also tried checking the MIME type of the image and it shows as image/tiff

Can someone suggest what I might be doing wrong?

like image 590
motatoes Avatar asked Dec 12 '25 08:12

motatoes


2 Answers

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.

like image 162
motatoes Avatar answered Dec 15 '25 15:12

motatoes


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)
like image 37
Pouya Avatar answered Dec 15 '25 15:12

Pouya