简体   繁体   English

在QLabel中的QPixmap上缩放QImage

[英]Zoom QImage on QPixmap in QLabel

I am subclassing QLabel, on which I set QPixmap. 我继承了QLabel的子类,在上面设置了QPixmap。 I want to zoom into the image displayed in the pixmap (without losing quality). 我想放大显示在像素图中的图像(不失画质)。 I don't want to see the entire image enlarged, just to zoom in. 我不想看到整个图像放大,只是放大。

I have tried many ways to scale the pixmap, but was unable to get good results. 我尝试了多种缩放像素图的方法,但无法获得良好的结果。 The following code resizes the image, but with very bad quality. 以下代码调整了图像的大小,但是质量很差。 What is the right way? 正确的方法是什么?

from PyQt5 import QtWidgets, QtCore, QtGui

class ImageLabel(QtWidgets.QLabel):

    def __init__(self, img):
        self.set_image(img)

    def set_image(self, image):
        qimg = QtGui.QPixmap.fromImage(image)
        self._displayed_pixmap = QtGui.QPixmap(qimg)
        # scale image to fit label
        self._displayed_pixmap.scaled(self.width(), self.height(), QtCore.Qt.KeepAspectRatio)
        self.setScaledContents(True)
        self.setMinimumSize(512, 512)
        self.show()

    def zoom_image(self):
        image_size = self._displayed_pixmap.size()
        image_size.setWidth(image_size.width() * 0.9)
        image_size.setHeight(image_size.height() * 0.9)
        self._displayed_pixmap = self._displayed_pixmap.scaled(image_size, QtCore.Qt.KeepAspectRatio)
        self.update()  # call paintEvent()

    def wheelEvent(self, event):
        modifiers = QtWidgets.QApplication.keyboardModifiers()
        if modifiers == QtCore.Qt.ControlModifier:
            self._zoom_image(event.angleDelta().y())

    def paintEvent(self, paint_event):
        painter = QtGui.QPainter(self)
        painter.drawPixmap(self.rect(), self._displayed_pixmap)

You can try using this: this work for me when I put pictures in PYQT 您可以尝试使用:当我将图片放在PYQT中时,此功能对我有用

self._displayed_pixmap.scaled(self.width(), self.height(), QtCore.Qt.SmoothTransformation)

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM