pyqt和opencv结合01:读取图像、显示

在这里插入图片描述

1 、opencv读取图像用于pyqt显示

python 复制代码
# image = cv2.imread(file_path)
   image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
      # 将图像转换为 Qt 可接受的格式
   height, width, channel = image.shape
   bytes_per_line = 3 * width

   q_image = QImage(image.data, width, height, bytes_per_line, QImage.Format_RGB888)
   pixmap = QPixmap.fromImage(q_image)
   self.image_label.setPixmap(pixmap)
   self.image_label.setScaledContents(True)

2、 opencv读取不了中文路径报错解决方式

python 复制代码
def read_chinese(file):
    image_numpy_data = cv2.imdecode(np.fromfile(file, dtype=np.uint8), -1)
    #返回numpy的ndarray
    return image_numpy_data

3 、pyqt中label默认不显示如何设置成显示

python 复制代码
self.image_label.setStyleSheet("border: 5px solid red;")

4、pyqt中label默认文字位于左侧如何设置成居中显示

python 复制代码
from PyQt5.QtCore import Qt
self.image_label.setAlignment(Qt.AlignCenter)

5、pyqt中label默认显示图像大小会裁剪如何设置成自适应

复制代码
    self.image_label.setScaledContents(True)

6、pyqt中打开文件对话框选择文件

复制代码
file_path, _ = QFileDialog.getOpenFileName(self, "Open Image File", "", "Image files (*.jpg *.png)")

以下是完整的代码:

python 复制代码
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton, QVBoxLayout, QWidget, QFileDialog
from PyQt5.QtGui import QPixmap, QImage
import cv2
import numpy as np

from PyQt5.QtCore import Qt


def read_chinese(file):
    image_numpy_data = cv2.imdecode(np.fromfile(file, dtype=np.uint8), -1)
    #返回numpy的ndarray
    return image_numpy_data

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

        self.setWindowTitle("Image Reader")
        self.setGeometry(100, 100, 400, 300)
        self.setMinimumSize(400, 300)  # 设置最小尺寸
        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)

        self.layout = QVBoxLayout()
        self.central_widget.setLayout(self.layout)

        self.image_label = QLabel()
        self.image_label.setText("图片展示区")
        self.image_label.setAlignment(Qt.AlignCenter)
        self.image_label.setStyleSheet("border: 5px solid red;")
        self.layout.addWidget(self.image_label)

        self.select_button = QPushButton("Select Image")
        self.select_button.clicked.connect(self.select_image)
        self.layout.addWidget(self.select_button)


    def select_image(self):
        file_path, _ = QFileDialog.getOpenFileName(self, "Open Image File", "", "Image files (*.jpg *.png)")
        if file_path:
            # 使用 OpenCV 读取图像FD
            # image = cv2.imread(file_path)
            # 将图像从 BGR 格式转换为 RGB 格式
            image = read_chinese(file_path)

            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            # 将图像转换为 Qt 可接受的格式
            height, width, channel = image.shape
            bytes_per_line = 3 * width

            q_image = QImage(image.data, width, height, bytes_per_line, QImage.Format_RGB888)
            pixmap = QPixmap.fromImage(q_image)
            self.image_label.setPixmap(pixmap)
            self.image_label.setScaledContents(True)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = ImageReaderApp()
    window.show()
    sys.exit(app.exec_())
相关推荐
谁在黄金彼岸3 分钟前
Ollama 还是 LM Studio:在 24GB 的 M6 Mac 上,我把 Ollama 卸载了
人工智能
方方洛5 分钟前
ray教程-18-性能调优与反模式
人工智能·分布式·机器学习
人工智能培训6 分钟前
知识图谱:结构化表达实体与关系的知识组织技术
大数据·人工智能·算法·重构·生活·知识图谱
GIS数据转换器17 分钟前
基于CIM的城市智慧供热监管服务平台
java·大数据·开发语言·人工智能·后端·物联网·struts
专注仿真17 分钟前
动态规划算法详解
人工智能·python·大模型训练·控制算法
xingyuzhisuan20 分钟前
不止文生图:StarverseAI 商业视觉解锁视频、展台全场景创作
人工智能
编程见习者26 分钟前
AI应用开发之LangGraph解析
人工智能·知识图谱
老唐77730 分钟前
大模型面试高频题:Transformer、MoE、RLHF、DPO 核心原理拆解
人工智能·深度学习·ai·大模型·transformer·智能体
m4Rk_34 分钟前
【论文阅读】Agent记忆机制(114):MemoryTax——当 Agent 能自主搜索历史,专门的记忆系统还划算吗?
论文阅读·人工智能·学习·开源·github
用户693717500138442 分钟前
同事相信 AI 给出的方案,我却不敢直接照做
人工智能