码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-03-27 21:18:16      阅读:535      评论:0      收藏:0      [点我收藏+]

标签:class   pyqt5   gets   enter   new   style   none   rgb   sys   

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显示

标签:class   pyqt5   gets   enter   new   style   none   rgb   sys   

原文地址:https://www.cnblogs.com/ACPIE-liusiqi/p/10610293.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!