close

上一篇文章: pyqt5+opencv開啟一張照片

想要進一步提升照片顯示的功能,當使用者調整form尺寸時能夠自動修改顯示圖片的大小

若圖片想要靠左對齊

 

 

 

 

 

 

顯示效果如下

 

若圖片想要置中對齊

 

> pyuic5 -x main.ui -o main_ui.py   

 

顯示效果如下

 

1. 如何偵測form尺寸變更事件呢?

開啟上一篇(pyqt5+opencv開啟一張照片)文章中main.py

PyQt_Main類別是繼承QMainWindow,在此可以覆寫(overloading method)

resizeEvent事件,如下

加入後,form一旦變更尺寸大小會自動印出resize

class PyQt_Main(QMainWindow):
    def __init__(self):                  
        QMainWindow.__init__(self)      
        self.ui = Ui_MainWindow()      
        self.ui.setupUi(self)            
        self.setWindowTitle("Load an image")
        self.ui.actionOpen.triggered.connect(lambda: self.actionOpenImage())
        self.show()                    
        return
   
    def resizeEvent(self, event):
        print("resize")

2. 如何調整影像顯示大小?

在actionOpenImage(self)中偷偷儲存pixmap

方便待會偵測到form尺寸變動時呼叫使用

儲存方式很簡單

self.pixmap = pixmap

因為self是實體本身遊走各個methods,因此可以偷偷掛入一個變數pixmap

 

def actionOpenImage(self):
        filePath = self.open_image_dialog()
        # filePath = "D:\OpenCV\images\lena.jpg"
        img = cv2.imread(filePath)
        # cv2.imshow("image", img)
        # Convert the image to RGB format
        img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
       
        # Create a QImage from the image data
        height, width, channels = img_rgb.shape
       
        q_image = QImage(
            img_rgb.data, width, height, channels * width, QImage.Format_RGB888
        )
        # Create a QPixmap from the QImage
        pixmap = QPixmap.fromImage(q_image)

        # Add pixmap to self in order to be used later
        self.pixmap = pixmap
       
        # Scale: keep aspect ratio
        pixmap = pixmap.scaled(self.ui.label_image.size(), aspectRatioMode=Qt.AspectRatioMode.KeepAspectRatio, transformMode=Qt.TransformationMode.SmoothTransformation)
        self.ui.label_image.setPixmap(pixmap)

 

3. 調整影像顯示大小

3.1先是抓出form的寬高self.width()和self.height()

3.2更新label_image至目前form的尺寸 setGeometry

3.3 抓取目前label_image的pixmap,如果該變數的資料型態非QPixmap表示還沒載入圖片

3.4 一旦載入圖片,意味著pixmap = self.pixmap有執行過 

      一定有剛剛偷偷儲存的pixmap變數

3.5 將pixmap縮放至label_image尺寸,同時保持圖片原本的顯示比例KeepAspectRatio

      附註: 由pixmap每次是由self.pixmap取出,因此會保有當初原本的解析度,不怕一連串的縮小視窗

3.6 將調整後pixmap再度顯示在label_image上

 

def resizeEvent(self, event):
        print("resize")
        w = self.width()
        h = self.height()
        self.ui.label_image.setGeometry(0, 0, w, h)
        pixmap = self.ui.label_image.pixmap()
        # print(type(pixmap))
        if(type(pixmap)==QtGui.QPixmap):
            pixmap = self.pixmap
            pixmap = pixmap.scaled(self.ui.label_image.size(), Qt.AspectRatioMode.KeepAspectRatio, Qt.TransformationMode.SmoothTransformation)
            self.ui.label_image.setPixmap(pixmap)
 

 

 4. 關於上述3.3 pixmap是否有值。可以用None判定更直覺

    def resizeEvent(self, event):
        # print("resize")
        w = self.width()
        h = self.height()
        self.ui.label_image.setGeometry(0, 0, w, h)
        pixmap = self.ui.label_image.pixmap()
        # print(type(pixmap))
        # if(type(pixmap)==QtGui.QPixmap):
        if(pixmap==None):
            return
        pixmap = self.pixmap
        pixmap = pixmap.scaled(self.ui.label_image.size(), Qt.AspectRatioMode.KeepAspectRatio, Qt.TransformationMode.SmoothTransformation)
        self.ui.label_image.setPixmap(pixmap)

 

 

 

arrow
arrow
    全站熱搜

    me1237guy 發表在 痞客邦 留言(0) 人氣()