Pyqt5 为什么我的图像加载为 PNG 而不是 JPG?

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

我正在使用 QPixmap 显示 jpg 图像,但它不显示。我将 jpg 图像转换为 png 并且有效,但我想知道它的原因。

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

加载为 jpg 看起来像这样

而 png 看起来像这样

编辑:

免责声明:我手动调整了 windows。

我运行之后:

 print(QtGui.QImageReader.supportedImageFormats())

输出为:

[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')]

编辑2:

整个程序:

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_())

问题是由您下载的图片引起的:https://www.tutorialspoint.com/pyqt/images/information.jpg扩展名不正确,它实际上是一张.png图片。这会导致 Qt 以及 PyQt 无法识别它,因此无法正确加载它。