import sys import cv2 from PyQt5.QtGui import QImage, QPixmap from PySide6.QtWidgets import QApplication, QLabel, QMainWindow def cv2_to_qimage(cv_img): """Convert an OpenCV image to QImage.""" height, width, channel = cv_img.shape bytes_per_line = 3 * width q_img = QImage(cv_img.data, width, height, bytes_per_line, QImage.Format_BGR888) return q_img class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Display Image with PySide6") self.resize(800, 600) # 使用 OpenCV 读取图像 cv_img = cv2.imread("./Image/1.png") # 替换为实际路径 img = cv_img.copy() # 转换为 QImage img = cv_img.copy() q_img = cv2_to_qimage(cv_img) # 创建 QLabel 并设置图像 label = QLabel(self) label.setPixmap(QPixmap.fromImage(q_img)) label.setScaledContents(True) label.setGeometry(50, 50, 700, 500) # 调整位置和大小 if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec())