pyqt5中设置图片显示等比例缩放

  • Post author:
  • Post category:其他


class LabelFrame(QLabel):
    def __init__(self, parent=None):
        super(LabelFrame, self).__init__()
        self.main_window = parent
        self.setAlignment(Qt.AlignCenter)  # 居中显示
        self.setMinimumSize(640, 480)
        # self.setScaledContents(True)

    def update_frame(self, frame):
        if self.height() > self.width():
            width = self.width()
            height = int(frame.shape[0] * (width / frame.shape[1]))
        else:
            height = self.height()
            width = int(frame.shape[1] * (height / frame.shape[0]))
        frame = cv2.resize(frame, (width, height))
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)  # bgr -> rgb
        h, w, c = frame.shape  # 获取图片形状
        image = QImage(frame, w, h, 3 * w, QImage.Format_RGB888)
        pix_map = QPixmap.fromImage(image)

        self.setPixmap(pix_map)



版权声明:本文为weixin_40841247原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。