当前位置:   article > 正文

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

pyqt5打包后graphicsview图片不显示

简而言之:

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

具体操作:

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

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

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

放置imageformats前运行结果:

放置imageformats后运行结果:

两个代码文件如下

使用PyUIC对在QT中布局之后的.ui文件生成的.py文件:untitled.py
  1. # -*- coding: utf-8 -*-
  2. # Form implementation generated from reading ui file 'untitled.ui'
  3. #
  4. # Created by: PyQt5 UI code generator 5.9.2
  5. #
  6. # WARNING! All changes made in this file will be lost!
  7. from PyQt5 import QtCore, QtGui, QtWidgets
  8. class Ui_Form(object):
  9. def setupUi(self, Form):
  10. Form.setObjectName("Form")
  11. Form.resize(1599, 1347)
  12. self.graphicsView = QtWidgets.QGraphicsView(Form)
  13. self.graphicsView.setGeometry(QtCore.QRect(200, 100, 1111, 1001))
  14. self.graphicsView.setObjectName("graphicsView")
  15. self.retranslateUi(Form)
  16. QtCore.QMetaObject.connectSlotsByName(Form)
  17. def retranslateUi(self, Form):
  18. _translate = QtCore.QCoreApplication.translate
  19. Form.setWindowTitle(_translate("Form", "Form"))

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

  1. from imgShow.untitled import Ui_Form
  2. from PyQt5.QtWidgets import QApplication, QWidget, QGraphicsPixmapItem, QGraphicsScene
  3. from PyQt5.QtGui import QImage, QPixmap
  4. import sys
  5. from PIL import Image
  6. class Img(QWidget, Ui_Form):
  7. def __init__(self):
  8. super(Img, self).__init__()
  9. self.setupUi(self)
  10. self.showImg()
  11. def showImg(self):
  12. picpath = "E:/图片/timgOQJ17LEP.jpg"
  13. img = QImage(picpath)
  14. pix = QPixmap.fromImage(img)
  15. item = QGraphicsPixmapItem(pix)
  16. scene = QGraphicsScene() # 创建场景
  17. scene.addItem(item)
  18. zoomscale = self.getPercent(picpath)
  19. item.setScale(zoomscale)
  20. self.graphicsView.setScene(scene)
  21. # 使图片保留原比例缩小,显示在graphicsView控件内,但不触发滚动条
  22. def getPercent(self, picpath):
  23. img = QImage(picpath)
  24. grap_w = self.graphicsView.width()
  25. grap_h = self.graphicsView.height()
  26. img_w = img.width() + 10
  27. img_h = img.height() + 10
  28. percent_w = float('%.4f' % (grap_w/img_w))
  29. percent_h = float('%.4f' % (grap_h/img_h))
  30. fun = lambda x, y: x if x < y else y
  31. percent = fun(percent_w, percent_h)
  32. return percent
  33. if __name__ == '__main__':
  34. app = QApplication(sys.argv)
  35. imgshow = Img()
  36. imgshow.show()
  37. app.exec_()

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/264494
推荐阅读
相关标签
  

闽ICP备14008679号