Search code examples
pythonpyqt5warningspython-3.7qpixmap

Detect QPixmap warning


I have list that contains image's path of a directory. Images are downloaded using requests having .jpg and .png extension mostly.

def display_image_label(self, image_path):
    self.scaled_image = QPixmap(image_path).scaled(600, 400, Qt.IgnoreAspectRatio, Qt.FastTransformation)
    self.image_label.setPixmap(self.scaled_image)

Now the issue is, few images that are .jpg doesn't load showing QPixmap::scaled: Pixmap is a null pixmap because the extension should have been .png because I have checked that changing the extension from .jpg to .png loads the image. I download the images using requests as it is, NO file formatting done.

Now, how do I catch the warning and act on it by changing the extension of the image ?


Solution

  • Use self.scaled_image.isNull() to check if scaling was successful or not like:

    if (not self.scaled_image.isNull()):
      #set it to label
    else:
      #do something else if you want