欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

pyqt5 读取numpy arrray 显示图片

程序员文章站 2022-05-28 11:07:51
...

目录

1、GUI界面(QT designer设计)

2、逻辑函数(回调等)

3、显示图片在label上

1)显示彩色图

彩色图显示色调不正常——opencv(BGR)QT(RGB)需要进行转换

2)显示灰度图


 

1、GUI界面(QT designer设计)

pyqt5 读取numpy arrray 显示图片

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'ImshowImgWithMatGUI.ui'
#
# Created by: PyQt5 UI code generator 5.15.1
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(280, 460, 201, 23))
        self.pushButton.setObjectName("pushButton")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(210, 90, 351, 291))
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "通过数组显示图片"))
        self.label.setText(_translate("MainWindow", "TextLabel"))



2、逻辑函数(回调等)

import cv2 as cv
from PyQt5.QtGui import QImage,QPixmap
from PyQt5.QtWidgets import QApplication, QMainWindow
from ImshowImgWithMatGUI import Ui_MainWindow
import sys

class My_UI(QMainWindow, Ui_MainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.setWindowTitle('窗口标题')
    def pushbuttonthing(self):
        # 读取彩色图片
        img_src = cv.imread(r"X:\4.jpg",)
        img_src = cv.cvtColor(img_src,cv.COLOR_BGR2RGB)
        # 读取label宽高
        label_width = self.label.width()
        label_height = self.label.height()

        # 将图片转换为QImage
        temp_imgSrc = QImage(img_src, img_src.shape[1], img_src.shape[0],img_src.shape[1]*3, QImage.Format_RGB888)
        # 将图片转换为QPixmap方便显示
        pixmap_imgSrc = QPixmap.fromImage(temp_imgSrc).scaled(label_width, label_height)

        # 使用label进行显示
        self.label.setPixmap(pixmap_imgSrc)

    def run(self):
        self.pushButton.clicked.connect(self.pushbuttonthing)
if __name__ == '__main__':
    app = QApplication(sys.argv)

    # 显示窗口
    win = My_UI()
    win.show()
    win.run()
    sys.exit(app.exec_())

3、显示图片在label上

1)显示彩色图

img_src是通过imread读取的彩色图像

QImage.Format_RGB888

# 将图片转换为QImage
        temp_imgSrc = QImage(img_src, img_src.shape[1], img_src.shape[0],img_src.shape[1]*3, QImage.Format_RGB888)
        # 将图片转换为QPixmap方便显示
        pixmap_imgSrc = QPixmap.fromImage(temp_imgSrc).scaled(label_width, label_height)

        # 使用label进行显示
        self.label.setPixmap(pixmap_imgSrc)

彩色图显示色调不正常——opencv(BGR)QT(RGB)需要进行转换

img_src = cv.cvtColor(img_src,cv.COLOR_BGR2RGB)

pyqt5 读取numpy arrray 显示图片 

pyqt5 读取numpy arrray 显示图片

2)显示灰度图

显示灰度图像(其中灰度图像是由彩色直接通过)

QImage.Format_Format_Grayscale8

# 将图片转换为QImage
        temp_imgSrc = QImage(img_src, img_src.shape[1], img_src.shape[0], QImage.Format_Format_Grayscale8)
        # 将图片转换为QPixmap方便显示
        pixmap_imgSrc = QPixmap.fromImage(temp_imgSrc).scaled(label_width, label_height)

        # 使用label进行显示
        self.label.setPixmap(pixmap_imgSrc)

 

 

相关标签: pyqt5 pyqt