S
S
sudo rm -rf /2019-01-14 10:32:11
Qt
sudo rm -rf /, 2019-01-14 10:32:11

Why does a QImage get ragged after scaled()?

On the main form, a QGraphicsView called paint_area of ​​size 400x400.
An object of the PaintScene(QGraphicsScene) class of the form

PaintScene Code
class PaintScene(QGraphicsScene):
    prev = None
    color = Qt.black
    width = 10
    strong = 10

    def __init__(self):
        super().__init__()
        pass

    def mousePressEvent(self, event):
        self.addEllipse(
            event.scenePos().x() - self.width / 2,
            event.scenePos().y() - self.width / 2,
            self.width,
            self.width,
            QPen(Qt.NoPen),
            QBrush(self.color))
        self.prev = event.scenePos()

    def mouseReleaseEvent(self, event):
        self.prev = None

    def mouseMoveEvent(self, event):
        if self.prev:
            self.addLine(
                self.prev.x(),
                self.prev.y(),
                event.scenePos().x(),
                event.scenePos().y(),
                QPen(self.color, self.width, Qt.SolidLine, Qt.RoundCap))
            self.prev = event.scenePos()

    def clear_area(self):
        self.clear()

The result is a canvas on which you can draw with the mouse. Drawings - set of lines and ellipses .
clearly
5c3c3a33b2ec1236645427.png

After drawing, the image is "saved" into a 30x30 icon by the get_image() function of the form
get_image code
def get_image(self):
        view = self.paint_area
        sceneRect = view.sceneRect().toRect()  #(0, 0, 400, 400)
        sceneRect.adjust(1, 1, -1, -1)  #(1, 1, 398, 398) удаляем рамку
        pixMap = view.grab(sceneRect)
        image = pixMap.toImage()
        image = image.scaled(30, 30)
        return image

However, the image is "torn" . Examples below.
400x400:
5c3c39886c431218411912.png
30x30:
5c3c39a2581cb310884506.png
How do I get the correct thumbnail?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question