PYQT5 打包后无法显示jpg图片问题

简而言之:

无法显示是因为缺少qjpeg.dll文件,找到含有qjpeg.dll文件的imageformats文件夹放到.exe文件所在目录。

具体操作:

使用everything(文件搜索工具)搜索一下,找到含有qjpeg.dll文件的路径

PYQT5 打包后无法显示jpg图片问题_第1张图片

找到之后直接将imageformats文件夹放在已经生成的.exe文件下就可以了

注意:千万是imageformats文件夹,而不是plugins,网上很多写的plugins文件夹,我放进去之后发现不能生效,不排除是我的环境问题

PYQT5 打包后无法显示jpg图片问题_第2张图片

放置imageformats前运行结果:

PYQT5 打包后无法显示jpg图片问题_第3张图片

放置imageformats后运行结果:

两个代码文件如下

使用PyUIC对在QT中布局之后的.ui文件生成的.py文件:untitled.py
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(1599, 1347)
        self.graphicsView = QtWidgets.QGraphicsView(Form)
        self.graphicsView.setGeometry(QtCore.QRect(200, 100, 1111, 1001))
        self.graphicsView.setObjectName("graphicsView")

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

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

需要打包的.py文件:img_show.py

from imgShow.untitled import Ui_Form
from PyQt5.QtWidgets import QApplication, QWidget, QGraphicsPixmapItem, QGraphicsScene
from PyQt5.QtGui import QImage, QPixmap
import sys
from PIL import Image

class Img(QWidget, Ui_Form):
    def __init__(self):
        super(Img, self).__init__()
        self.setupUi(self)
        self.showImg()

    def showImg(self):
        picpath = "E:/图片/timgOQJ17LEP.jpg"
        img = QImage(picpath)
        pix = QPixmap.fromImage(img)
        item = QGraphicsPixmapItem(pix)
        scene = QGraphicsScene()  # 创建场景
        scene.addItem(item)
        zoomscale = self.getPercent(picpath)
        item.setScale(zoomscale)
        self.graphicsView.setScene(scene)

    # 使图片保留原比例缩小,显示在graphicsView控件内,但不触发滚动条
    def getPercent(self, picpath):   
        img = QImage(picpath)
        grap_w = self.graphicsView.width()
        grap_h = self.graphicsView.height()
        img_w = img.width() + 10
        img_h = img.height() + 10
        percent_w = float('%.4f' % (grap_w/img_w))
        percent_h = float('%.4f' % (grap_h/img_h))
        fun = lambda x, y: x if x < y else y
        percent = fun(percent_w, percent_h)
        return percent


if __name__ == '__main__':
    app = QApplication(sys.argv)
    imgshow = Img()
    imgshow.show()
    app.exec_()

 

你可能感兴趣的:(pyqt,pyqt5,QT)