将opencv的imread()函数读取的图片用QLabel显示

Posted acpie-liusiqi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将opencv的imread()函数读取的图片用QLabel显示相关的知识,希望对你有一定的参考价值。

switchPicture.py

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import numpy as np
import cv2
import sys

class MyWidget(QWidget):
    def __init__(self, parent = None):
        super().__init__(parent)
        self.setWindowTitle(self.tr(显示图片))
        self.resize(500,400)
        self.label = QLabel(self)
        self.label.setFrameShape(QFrame.Box)
        self.label.setAlignment(Qt.AlignCenter)
        img = cv2.imread(girl_1.jpg)
        #cv2.imshow(‘111‘,img)
        #cv2.waitKey(0)
        width = img.shape[1]
        height = img.shape[0]
        print(cv2, width: +str(width)+ height: +str(height))
        cv2.cvtColor(img, cv2.COLOR_BGR2RGB,img)
        qt_img = QImage(img.data,width,height,QImage.Format_RGB888)
        #print(type(qt_img))

        #self.label.setPixmap(QPixmap.fromImage(qt_img))

        self.label.setGeometry(0, 0, 400, 300)
        n_width = qt_img.width()
        n_height = qt_img.height()
        print(Qt, width: +str(n_width)+ height: +str(n_height))
        if n_width / 400 >= n_height / 300:
            ratio = n_width / 400
        else:
            ratio = n_height / 300
        new_width = n_width / ratio
        new_height = n_height / ratio
        new_img = qt_img.scaled(new_width, new_height, Qt.KeepAspectRatio)
        self.label.setPixmap(QPixmap.fromImage(new_img))




if __name__ == __main__:
    app = QApplication(sys.argv)
    widget = MyWidget()
    widget.show()
    #print(widget.children())
    sys.exit(app.exec_())

技术图片

以上是关于将opencv的imread()函数读取的图片用QLabel显示的主要内容,如果未能解决你的问题,请参考以下文章

opencv用imread( argv[1], 1)读取图片

PIL.Image.open 与 cv2.imread 读取的图片在像素点上不一致

Python下的OpenCV学习 02 —— 图像的读取与输出

opencv

opencv入门基础——图像读取,图像显示,图像保存

opencv系列02OpenCV4.X图像读取与显示