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

时间:2017-06-22 19:26:48

标签: python pyqt pyqt5 qimage

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

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

像jpg一样加载

enter image description here

虽然png看起来像这样

enter image description here

修改

免责声明:我手动调整了窗口大小。

我跑完后:

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

EDIT2:

整个计划:

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

1 个答案:

答案 0 :(得分:2)

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