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_())
相关推荐
AI完全体12 分钟前
【AI知识点】偏差-方差权衡(Bias-Variance Tradeoff)
人工智能·深度学习·神经网络·机器学习·过拟合·模型复杂度·偏差-方差
GZ_TOGOGO24 分钟前
【2024最新】华为HCIE认证考试流程
大数据·人工智能·网络协议·网络安全·华为
sp_fyf_202424 分钟前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-02
人工智能·神经网络·算法·计算机视觉·语言模型·自然语言处理·数据挖掘
新缸中之脑26 分钟前
Ollama 运行视觉语言模型LLaVA
人工智能·语言模型·自然语言处理
胡耀超1 小时前
知识图谱入门——3:工具分类与对比(知识建模工具:Protégé、 知识抽取工具:DeepDive、知识存储工具:Neo4j)
人工智能·知识图谱
陈苏同学1 小时前
4. 将pycharm本地项目同步到(Linux)服务器上——深度学习·科研实践·从0到1
linux·服务器·ide·人工智能·python·深度学习·pycharm
唐家小妹1 小时前
介绍一款开源的 Modern GUI PySide6 / PyQt6的使用
python·pyqt
吾名招财1 小时前
yolov5-7.0模型DNN加载函数及参数详解(重要)
c++·人工智能·yolo·dnn
鼠鼠龙年发大财2 小时前
【鼠鼠学AI代码合集#7】概率
人工智能
龙的爹23332 小时前
论文 | Model-tuning Via Prompts Makes NLP Models Adversarially Robust
人工智能·gpt·深度学习·语言模型·自然语言处理·prompt