继续上一篇博客Ubuntu下Pycharm配置pyqt5来做界面_jiugeshao的专栏-CSDN博客,在界面上加入图像的显示功能。
通过命令来安装python下的opencv库
pip install opencv-python
安装完毕后,继续在前面博客设计的界面上加入QLabel控件
设计完毕后,重新生成对应的form.py文件
修改前面博客testgui2.py中的代码如下:
#!/usr/bin/env python # -*- coding: utf-8 -*- # author:Icecream.Shao from form import Ui_Form from PyQt5 import QtWidgets import sys import cv2 from PyQt5.Qt import QImage from PyQt5.Qt import QPixmap class Mywindow(QtWidgets.QMainWindow, Ui_Form): def __init__(self): super(Mywindow, self).__init__() self.setupUi(self) def slot1(self): self.textEdit.setText(("hi,helloworld~")) image = cv2.imread("/home/icecreamshao/108.bmp") dstimage = cv2.cvtColor(image, cv2.COLOR_BGR2RGB); Qtemp = QImage(dstimage[:], dstimage.shape[1], dstimage.shape[0], dstimage.shape[1] * 3, QImage.Format_RGB888) pixmap_Qtemp = QPixmap.fromImage(Qtemp) # pixmap_Qtemp = QPixmap.fromImage(Qtemp).scaled(self.label.width(), self.label.height()) self.label.setPixmap(pixmap_Qtemp) self.label.resize(Qtemp.size()) app = QtWidgets.QApplication(sys.argv) window = Mywindow() window.show() sys.exit(app.exec_())
运行程序,结果如下: