3

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?

8
  • 1
    Note that "TIFF" is a complicated and very flexible file container format, so most libraries that can "handle TIFF" can't actually handle all legal TIFF files. For example, some libraries can't do FAX compression, or CMYK color, or 24bpp RGB, or tiled files, or… Commented Jun 7, 2013 at 18:58
  • After downloading and checking out this file, it seems to be about as common as can be—a single image with LZW (#5) compression, 96dpi, RGB at 8bpp, no color profile, little-endian 32-bit offsets, … So I'm not so sure the usual "Thousands of Incompatible File Formats" issue is to blame here. Commented Jun 7, 2013 at 19:03
  • @abarnert It's weird because This post seems to have the similar problem with a .jpg image .. Commented Jun 7, 2013 at 19:11
  • When I try this with Qt 4.8.4 and 5.0.1 (from Homebrew, on 64-bit Mac OS X 10.8), they both have no problem displaying the image, or scaling it to any size I want, so… What platform, and what version of Qt, are you using? Commented Jun 7, 2013 at 19:11
  • I'm on 32-bit Windows, using winpython for python 2.7 Commented Jun 7, 2013 at 19:14

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The second argument should be a valid image format from one of the values listed at - pyqt.sourceforge.net/Docs/PyQt4/…
1

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)

1 Comment

It returns a tuple for PySide, but returns a string for PyQt. That was not my problem, sorry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.