本文主要是介绍PYQT5笔记 008 :图片读出界面程序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
直接显示版
主要代码:
pixmap = QPixmap(r"C:\Users\Administrator\Desktop\cat.jpg")
self.label2.setPixmap(pixmap)
总体实现:
# https://pythonspot.com/pyqt5-image/的例子
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtGui import QIcon, QPixmapclass App(QWidget):def __init__(self):super().__init__()self.title = 'PyQt5 image - pythonspot.com'self.left = 10self.top = 10self.width = 640self.height = 480self.initUI()def initUI(self):self.setWindowTitle(self.title)self.setGeometry(self.left, self.top, self.width, self.height)# Create widgetlabel = QLabel(self)pixmap = QPixmap(r"C:\Users\Administrator\Desktop\cat.jpg")label.setPixmap(pixmap)self.resize(pixmap.width(), pixmap.height())self.show()if __name__ == '__main__':app = QApplication(sys.argv)ex = App()sys.exit(app.exec_())
运行后结果(按照pythonspot的例子):
互动版
# -*- coding: utf-8 -*-# Form implementation generated from reading ui file '1.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QFont, QPixmap
from PyQt5.QtWidgets import QSplashScreen, QFileDialog
import cv2class Ui_MainWindow(object):def setupUi(self, MainWindow):MainWindow.setObjectName("MainWindow")MainWindow.resize(640, 480)self.centralwidget = QtWidgets.QWidget(MainWindow)self.centralwidget.setObjectName("centralwidget")self.label = QtWidgets.QLabel(self.centralwidget)self.label.setGeometry(QtCore.QRect(90, 30, 481, 261))self.label.setObjectName("label")self.pushButton = QtWidgets.QPushButton(self.centralwidget)self.pushButton.setGeometry(QtCore.QRect(270, 350, 93, 28))self.pushButton.setObjectName("pushButton")# TODO 为按钮添加处理函数self.pushButton.clicked.connect(self.openimage)# 注意函数不加括号MainWindow.setCentralWidget(self.centralwidget)self.menubar = QtWidgets.QMenuBar(MainWindow)self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 26))self.menubar.setObjectName("menubar")MainWindow.setMenuBar(self.menubar)self.statusbar = QtWidgets.QStatusBar(MainWindow)self.statusbar.setObjectName("statusbar")MainWindow.setStatusBar(self.statusbar)self.retranslateUi(MainWindow)QtCore.QMetaObject.connectSlotsByName(MainWindow)def retranslateUi(self, MainWindow):_translate = QtCore.QCoreApplication.translateMainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))self.label.setText(_translate("MainWindow", "TextLabel"))self.pushButton.setText(_translate("MainWindow", "PushButton"))def openimage(self):print(111222333)# The QFileDialog class provides a dialog that allow users to select files or directories https://doc.qt.io/qt-5/qfiledialog.htmlimgName, imgType = QFileDialog.getOpenFileName(None, "打开图片", "", "*.jpg;;*.png;;All Files(*)")# self 报错 [读取文件的参数设置self报错,参考本文改为了None](https://www.it1352.com/1829899.html)if imgName!='':self.imgname1=imgName# print("imgName",imgName,type(imgName))self.image=cv2.imread(imgName)width = self.image.shape[1]height = self.image.shape[0]# 设置新的图片分辨率框架width_new = 700height_new = 500# 判断图片的长宽比率if width / height >= width_new / height_new:show = cv2.resize(self.image, (width_new, int(height * width_new / width)))else:show = cv2.resize(self.image, (int(width * height_new / height), height_new))image = cv2.cvtColor(show, cv2.COLOR_RGB2BGR)showImage = QtGui.QImage(image, image.shape[1], image.shape[0], 3 * image.shape[1], QtGui.QImage.Format_RGB888)self.label.setPixmap(QtGui.QPixmap.fromImage(showImage))import sys
# 主方法,程序从此处启动PyQt设计的窗体
if __name__ == '__main__':app = QtWidgets.QApplication(sys.argv)MainWindow = QtWidgets.QMainWindow() # 创建窗体对象ui = Ui_MainWindow() # 创建PyQt设计的窗体对象ui.setupUi(MainWindow) # 调用PyQt窗体的方法对窗体对象进行初始化设置MainWindow.show() # 显示窗体sys.exit(app.exec_()) # 程序关闭时退出进程
# QImage
[qimage文档](https://doc.qt.io/qtforpython-5/PySide2/QtGui/QImage.html)
# QPixmap
[PyQt5 Tutorial - Images and QPixmap](https://www.youtube.com/watch?v=D0iCHFXHb_g)[How to make OpenCV and PyQt5 based GUI for image processing applications](https://pyshine.com/Make-GUI-for-OpenCv-And-PyQt5/)[PyQt5 How To Add Image In PyQt Window](https://codeloop.org/pyqt5-how-to-add-image-in-pyqt-window/)# qlabel
[Python QLabel.setPixmap Examples](https://python.hotexamples.com/examples/PyQt4.QtGui/QLabel/setPixmap/python-qlabel-setpixmap-method-examples.html)
[qlabel官方文档,在Public Slots有方法setMovie,setPicture,setPixmap](https://doc.qt.io/qt-5/qlabel.html)
[https://forum.qt.io/topic/82420/show-picture-using-qlabel-and-pixmap](https://forum.qt.io/topic/82420/show-picture-using-qlabel-and-pixmap)
# 其他
[插入matplotlib图片至GUI界面(不产生临时文件)](https://www.bilibili.com/video/BV1Bi4y1u7XM?)
img = Image.open(io.BytesIO(buffer.getvalue()))
label.setPixmap(img.toqpixmap())
这篇关于PYQT5笔记 008 :图片读出界面程序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!