PyQt5显示图片(Qlabel)兼容性处理方式

基本思路

1.根据设计的qlabel的尺寸,最好是和图片等比例的, 长和宽的比例。

2.将图片进行放缩,以满足正常显示

                # 将图像进行缩放,以满足画布label的显示
                show_width = display_img.geometry().size().width()
                show_height = display_img.geometry().size().height()
                _src = cv2.resize(src,(show_width,show_height))

3.将图像矩阵转换成Qimage

    @ staticmethod
    def display_image(image):
        if len(image.shape) == 3:
            Q_img = QImage(image.copy(),
                           image.shape[1],
                           image.shape[0],
                           image.shape[1] * 3,
                           QImage.Format_RGB888)
        elif len(image.shape) == 1:
            Q_img = QImage(image.copy(),
                           image.shape[1],
                           image.shape[0],
                           QImage.Format_Indexed8)
        else:
            Q_img = QImage(image.copy(),
                           image.shape[1],
                           image.shape[0],
                           QImage.Format_RGB888)
        return Q_img

4.将qimage设置在相应的qlabel上

display_img.setPixmap(QtGui.QPixmap(qimage))

猜你喜欢

转载自blog.csdn.net/weixin_44503976/article/details/130217004