summaryrefslogtreecommitdiff
path: root/opencv-qt-integration-1/python/ImageViewer.py
blob: 1525f2120f48c29dcd137175e174a16ee7bed46f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python
# encoding: utf-8

import os
import sys

try:
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from PyQt5.QtWidgets import *
    import cv2
except ImportError:
    print("Please install the required packages.")
    sys.exit()

class ImageViewer(QWidget):

    def __init__(self):
        QWidget.__init__(self)
        self.filename = "../../assets/flughahn.jpg"
        self.setup_ui()

    def setup_ui(self):
        img = cv2.imread(self.filename)
        self.image_label = 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.image_label.setPixmap(QPixmap.fromImage(img_))
        self.quit_button = QPushButton("Quit")
        self.quit_button.clicked.connect(self.close)
        self.main_layout = QVBoxLayout()
        self.main_layout.addWidget(self.image_label)
        self.main_layout.addWidget(self.quit_button)
        self.setLayout(self.main_layout)
        self.setWindowTitle("OpenCV - Qt Integration")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    viewer = ImageViewer()
    viewer.show()
    app.exec_()