A Pyside6 template that displays images

from PySide6.QtCore import Qt
from numpy import ndarray
from PySide6.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget
from PySide6.QtGui import QPixmap
from PIL import Image, ImageQt

class ImageViewer(QMainWindow):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.setWindowTitle('Image Viewer')
        self.setGeometry(100, 100, 300, 300)

        layout = QVBoxLayout()

        # 创建一个 QLabel 用于显示图像
        self.label = QLabel(self)
        # 顶部对齐
        self.label.setAlignment(Qt.AlignTop)
        layout.addWidget(self.label)

        widget = QWidget()
        widget.setLayout(layout)
        self.setCentralWidget(widget)

    def updateImage(self, image_data: Image):
        # 转一下
        q_image = ImageQt.ImageQt(image_data)

        # 将 QImage 转换成 QPixmap,并显示在 QLabel 中
        pixmap = QPixmap.fromImage(q_image)
        self.label.setPixmap(pixmap)

use
 

if __name__ == '__main__':
   
    .....

    # 用Qt动态显示一下图像
    app = QApplication(sys.argv)
    viewer = ImageViewer()
    viewer.show()

    image_container = ImageContainer(300, 100)

    while True:
        try:
            image: ndarray = camera.get_image()
            if image is not None:
                
                container_image = image_container.get_base_container_image()
                viewer.updateImage(container_image)

            key = cv2.waitKey(50) & 0xFF
            if key == ord('e') or key == ord('E'):
                cv2.destroyAllWindows()
                break
        except Exception as e:
            print(e)

    sys.exit(app.exec())

Guess you like

Origin blog.csdn.net/wenxingchen/article/details/133797570