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_())
相关推荐
吃好睡好便好几秒前
矩阵秩的计算
人工智能·学习·线性代数·算法·机器学习·matlab·矩阵
诸葛务农3 分钟前
溶液纳米颗粒净化技术及其在光刻胶纳过滤和提纯中的应用(下)
人工智能
计算机安禾3 分钟前
【算法分析与设计】第35篇:后缀数据结构:后缀树与后缀数组的构造
大数据·人工智能·算法·机器学习·剪枝
RockHopper20253 分钟前
从结构化建模到动态语义映射:AI时代企业软件系统的重构逻辑
人工智能·智能体·语义驱动·运行语义
玩c#的小杜同学4 分钟前
未来 AI 会装进电脑里吗?本地 AI、AI PC 和企业隐私计算
人工智能·微软·c#·电脑·英伟达
weixin_468466855 分钟前
深度学习损失函数新手实战指南
人工智能·python·深度学习·算法·机器学习·ai
医学AI望远镜9 分钟前
TMI | 噪声标签学习新思路:先判断样本可信度,再筛选体素监督
人工智能·医学+ai
-嘟囔着拯救世界-9 分钟前
Claude Code 平替来了?DeepSeek-TUI 保姆级安装教程
人工智能·ai·ai编程·deepseek·vibecoding·deepseek-tui
IT_陈寒11 分钟前
Vue的动态组件坑了我整整一天!
前端·人工智能·后端
ViiTor_AI13 分钟前
原声克隆在视频出海中的作用:3个关键细节
人工智能