Jaitnium
Jaitnium

Reputation: 650

Pyqt5 Why does my image load as a PNG but not a JPG?

I'm using QPixmap to display an jpg image but it doesn't display. I converted the jpg image into a png and that works but I'd like to know the reason for it.

pixmap = QtGui.QPixmap("information.jpg")
self.infoLabel.setPixmap(pixmap)
self.infoLabel.resize(100, 100)

Loading as a jpg looks like this

enter image description here

while a png looks like this

enter image description here

EDIT:

Disclaimer: I manually resized the windows.

After I ran:

 print(QtGui.QImageReader.supportedImageFormats())

The output was:

[PyQt5.QtCore.QByteArray(b'bmp'), PyQt5.QtCore.QByteArray(b'cur'), PyQt5.QtCore.QByteArray(b'gif'), PyQt5.QtCore.QByteArray(b'icns'), PyQt5.QtCore.QByteArray(b'ico'), PyQt5.QtCore.QByteArray(b'jpeg'), PyQt5.QtCore.QByteArray(b'jpg'), PyQt5.QtCore.QByteArray(b'pbm'), PyQt5.QtCore.QByteArray(b'pgm'), PyQt5.QtCore.QByteArray(b'png'), PyQt5.QtCore.QByteArray(b'ppm'), PyQt5.QtCore.QByteArray(b'svg'), PyQt5.QtCore.QByteArray(b'svgz'), PyQt5.QtCore.QByteArray(b'tga'), PyQt5.QtCore.QByteArray(b'tif'), PyQt5.QtCore.QByteArray(b'tiff'), PyQt5.QtCore.QByteArray(b'wbmp'), PyQt5.QtCore.QByteArray(b'webp'), PyQt5.QtCore.QByteArray(b'xbm'), PyQt5.QtCore.QByteArray(b'xpm')]

Edit2:

Entire program:

from PyQt5 import QtCore, QtGui, QtWidgets
import sys

class Ui_Form(QtWidgets.QWidget):
    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        self.setupUi(self)

    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 400)

        self.verticalLayout = QtWidgets.QVBoxLayout(Form)
        self.verticalLayout.setObjectName("verticalLayout")

        #Label as image code
        self.infoLabel = QtWidgets.QLabel(Form)
        pixmap = QtGui.QPixmap("information.jpg")
        self.infoLabel.setPixmap(pixmap)
        self.infoLabel.resize(100, 100)
        print(QtGui.QImageReader.supportedImageFormats()) 
        self.verticalLayout.addWidget(self.infoLabel)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Test"))

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ex = Ui_Form()
    ex.show()
    sys.exit(app.exec_())

Upvotes: 2

Views: 2961

Answers (1)

eyllanesc
eyllanesc

Reputation: 243897

The problem is caused by the image you downloaded : https://www.tutorialspoint.com/pyqt/images/information.jpg has an inadequate extension, it is actually a .png image. This causes Qt, and therefore PyQt, not to be able to recognize it, and therefore does not load it correctly.

Upvotes: 4

Related Questions