Display images dynamically using a Label control.
Use Qt Designer to design a simple interface, add a display control label, and modify the object name to LabelDisplayImg in the properties on the right. Save it as cycle.ui file.
Use the extension tool to generate the corresponding cycle.py file.
# cycle.py文件
# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file 'cycle.ui'
##
## Created by: Qt User Interface Compiler version 5.15.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
if not MainWindow.objectName():
MainWindow.setObjectName(u"MainWindow")
MainWindow.resize(500, 400)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName(u"centralwidget")
self.labelDisplayImg = QLabel(self.centralwidget)
self.labelDisplayImg.setObjectName(u"labelDisplayImg")
self.labelDisplayImg.setGeometry(QRect(20, 20, 400, 300))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QMenuBar(MainWindow)
self.menubar.setObjectName(u"menubar")
self.menubar.setGeometry(QRect(0, 0, 500, 22))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QStatusBar(MainWindow)
self.statusbar.setObjectName(u"statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
# setupUi
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
self.labelDisplayImg.setText(QCoreApplication.translate("MainWindow", u"TextLabel", None))
# retranslateUi
Next write the main function
from PySide2.QtWidgets import QApplication, QMainWindow
from PySide2.QtCore import QTimer
from PySide2.QtGui import QPixmap, QImage
from ui.cycle import Ui_MainWindow #此处ui是存放cycle.py的文件夹,根据自己路径写即可
import cv2
import sys
import glob
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
# 获取路径下所有jpg图像的路径
self.images = glob.glob('../images/*.jpg')
self.n = 0
self.timer = QTimer(self)
def window_init(self):
show_img(self.images[self.n])
self.labelDisplayImg.setScaledContents(True)
self.timer.timeout.connect(self.timer_TimeOut)
self.timer.start(500)
def timer_TimeOut(self):
if self.n >= (len(self.images) - 1):
# self.n = 0 # 循环显示
self.timer.stop() # 到设置数量停止
show_img(self.images[self.n])
self.n += 1
# 设置label显示图像函数
def show_img(img):
img = cv2.imread(img)
imgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
imgc = QImage(imgb.data, imgb.shape[1], imgb.shape[0], imgb.shape[1]*3, QImage.Format_RGB888)
window.labelDisplayImg.setPixmap(QPixmap.fromImage(imgc))
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.window_init()
window.show()
app.exec_()
Here, the image path is set in the function. You can also set a menu or button to realize the selected folder, read the image cycle or display until the set number of images stops. If you are interested, you can try it yourself.