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_())
相关推荐
z_mazin1 小时前
反爬虫机制中的验证码识别:类型、技术难点与应对策略
人工智能·计算机视觉·目标跟踪
lixy5792 小时前
深度学习3.7 softmax回归的简洁实现
人工智能·深度学习·回归
youhebuke2252 小时前
利用deepseek快速生成甘特图
人工智能·甘特图·deepseek
訾博ZiBo2 小时前
AI日报 - 2025年04月26日
人工智能
郭不耐2 小时前
DeepSeek智能时空数据分析(三):专业级地理数据可视化赏析-《杭州市国土空间总体规划(2021-2035年)》
人工智能·信息可视化·数据分析·毕业设计·数据可视化·城市规划
AI军哥2 小时前
MySQL8的安装方法
人工智能·mysql·yolo·机器学习·deepseek
余弦的倒数3 小时前
知识蒸馏和迁移学习的区别
人工智能·机器学习·迁移学习
Allen Bright3 小时前
【机器学习-线性回归-2】理解线性回归中的连续值与离散值
人工智能·机器学习·线性回归
青松@FasterAI3 小时前
【程序员 NLP 入门】词嵌入 - 上下文中的窗口大小是什么意思? (★小白必会版★)
人工智能·自然语言处理
AIGC大时代4 小时前
高效使用DeepSeek对“情境+ 对象 +问题“型课题进行开题!
数据库·人工智能·算法·aigc·智能写作·deepseek