0

I'm trying to create a GUI that display an image. i don't know how to show an image in Pyqt label.

Here is my OpenCV simple code:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from dialog import Ui_MainWindow
import cv2 as cv2


class MyApp(QMainWindow):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.filename = 'lin-manuel-miranda.png'
        self.setup_ui()
        # Events
        self.ui.pushButton.clicked.connect(self.insertText)

    def insertText(self):
        t = self.ui.label.text()
        self.ui.lineEdit.setText(t)

    def setup_ui(self):
        img = cv2.imread(self.filename)
        self.ui.image = QLabel()
        if img is None:
            self.image_label.setText("Cannot load the input image.")
        else:
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            img_ = QImage(img.data, img.shape[1], img.shape[0], QImage.Format_RGB888)
            self.ui.image.setPixmap(QPixmap.fromImage(img_))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    myapp = MyApp()
    myapp.show()
    app.exec_()

i'm using Opencv version 4.1.0, and python version 3.7.3

Cakkaphong Khangsri
  • 154
  • 1
  • 2
  • 10

0 Answers0