Python PyQt5.QtGui.QPainter.SmoothPixmapTransform() Examples

The following are 14 code examples of PyQt5.QtGui.QPainter.SmoothPixmapTransform(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module PyQt5.QtGui.QPainter , or try the search function .
Example #1
Source File: CLoadingBar.py    From CustomWidgets with GNU General Public License v3.0 6 votes vote down vote up
def paintEvent(self, _):
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing, True)
        painter.setRenderHint(QPainter.HighQualityAntialiasing, True)
        painter.setRenderHint(QPainter.SmoothPixmapTransform, True)
        # 背景
        painter.fillRect(self.rect(), Qt.transparent)
        # 进度块
        ratio = (self.value() - self.minimum()) / \
            (self.maximum() - self.minimum())
        width = self.rect().width() * ratio
        if self.isError:
            color = QColor(self._failedColor or CLoadingBar.FailedColor)
        else:
            color = QColor(self._color or CLoadingBar.Color)
        color.setAlpha(self._alpha)
        painter.setBrush(color)
        painter.setPen(Qt.NoPen)
        painter.drawRoundedRect(QRectF(0, 0, width, self.height()), 2, 2) 
Example #2
Source File: QtNinePatch2.py    From PyQt with GNU General Public License v3.0 6 votes vote down vote up
def createPixmapFromNinePatchImage(image, dw, dh):
    w = dw
    h = dh
    if w < image.width() or h < image.height():  # shrink
        w = max(image.width(), w)
        h = max(image.height(), h)
        pm1 = resize9patch(image, w, h)
        if pm1.isNull():
            return QPixmap()
        pm2 = QPixmap(dw, dh)
        pm2.fill(Qt.transparent)
        pr = QPainter(pm2)
        pr.setRenderHint(QPainter.Antialiasing)
        pr.setRenderHint(QPainter.SmoothPixmapTransform)
        pr.drawPixmap(0, 0, dw, dh, pm1, 0, 0, w, h)
        return pm2
    else:
        return resize9patch(image, dw, dh) 
Example #3
Source File: stimulus_display.py    From stytra with GNU General Public License v3.0 5 votes vote down vote up
def paintEvent(self, QPaintEvent):
        """Generate the stimulus that will be displayed. A QPainter object is
        defined, which is then passed to the current stimulus paint function
        for drawing the stimulus.

        Parameters
        ----------
        QPaintEvent :
            

        Returns
        -------

        """
        p = QPainter(self)
        p.setBrush(QBrush(QColor(0, 0, 0)))
        w = self.width()
        h = self.height()

        if self.protocol_runner is not None:
            if self.protocol_runner.running:
                try:
                    self.protocol_runner.current_stimulus.paint(p, w, h)
                except AttributeError:
                    pass
            else:
                p.drawRect(QRect(-1, -1, w + 2, h + 2))
                p.setRenderHint(QPainter.SmoothPixmapTransform, 1)
                if self.img is not None:
                    p.drawImage(QPoint(0, 0), self.img)

        if self.calibrator is not None:
            if self.calibrator.enabled:
                self.calibrator.paint_calibration_pattern(p, h, w)

        p.end() 
Example #4
Source File: stimulus_display.py    From stytra with GNU General Public License v3.0 5 votes vote down vote up
def paintEvent(self, QPaintEvent):

        p = QPainter(self)
        p.setBrush(QBrush(QColor(0, 0, 0)))

        w = self.width()
        h = self.height()

        if self.display_state:

            if self.protocol_runner is not None:
                if self.protocol_runner.running:
                    try:
                        self.protocol_runner.current_stimulus.paint(p, w, h)
                    except AttributeError:
                        pass
                else:
                    p.drawRect(QRect(-1, -1, w + 2, h + 2))
                    p.setRenderHint(QPainter.SmoothPixmapTransform, 1)
                    if self.img is not None:
                        p.drawImage(QPoint(0, 0), self.img)

            if self.calibrator is not None:
                if self.calibrator.enabled:
                    self.calibrator.paint_calibration_pattern(p, h, w)

        p.end() 
Example #5
Source File: meta.py    From FeelUOwn with GNU General Public License v3.0 5 votes vote down vote up
def paintEvent(self, e):
        """
        draw pixmap with border radius

        We found two way to draw pixmap with border radius,
        one is as follow, the other way is using bitmap mask,
        but in our practice, the mask way has poor render effects
        """
        if self._pixmap is None:
            return
        radius = 3
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setRenderHint(QPainter.SmoothPixmapTransform)
        scaled_pixmap = self._pixmap.scaledToWidth(
            self.width(),
            mode=Qt.SmoothTransformation
        )
        size = scaled_pixmap.size()
        brush = QBrush(scaled_pixmap)
        painter.setBrush(brush)
        painter.setPen(Qt.NoPen)
        y = (size.height() - self.height()) // 2
        painter.save()
        painter.translate(0, -y)
        rect = QRect(0, y, self.width(), self.height())
        painter.drawRoundedRect(rect, radius, radius)
        painter.restore()
        painter.end() 
Example #6
Source File: CAvatar.py    From CustomWidgets with GNU General Public License v3.0 5 votes vote down vote up
def paintEvent(self, event):
        super(CAvatar, self).paintEvent(event)
        # 画笔
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing, True)
        painter.setRenderHint(QPainter.HighQualityAntialiasing, True)
        painter.setRenderHint(QPainter.SmoothPixmapTransform, True)
        # 绘制
        path = QPainterPath()
        diameter = min(self.width(), self.height())
        if self.shape == self.Circle:
            radius = int(diameter / 2)
        elif self.shape == self.Rectangle:
            radius = 4
        halfW = self.width() / 2
        halfH = self.height() / 2
        painter.translate(halfW, halfH)
        path.addRoundedRect(
            QRectF(-halfW, -halfH, diameter, diameter), radius, radius)
        painter.setClipPath(path)
        # 如果是动画效果
        if self.rotateAnimation.state() == QPropertyAnimation.Running:
            painter.rotate(self._angle)  # 旋转
            painter.drawPixmap(
                QPointF(-self.pixmap.width() / 2, -self.pixmap.height() / 2), self.pixmap)
        else:
            painter.drawPixmap(-int(halfW), -int(halfH), self.pixmap)
        # 如果在加载
        if self.loadingTimer.isActive():
            diameter = 2 * self.pradius
            painter.setBrush(
                QColor(45, 140, 240, (1 - self.pradius / 10) * 255))
            painter.setPen(Qt.NoPen)
            painter.drawRoundedRect(
                QRectF(-self.pradius, -self.pradius, diameter, diameter), self.pradius, self.pradius) 
Example #7
Source File: CColorControl.py    From CustomWidgets with GNU General Public License v3.0 5 votes vote down vote up
def paintEvent(self, event):
        super(CColorControl, self).paintEvent(event)
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing, True)
        painter.setRenderHint(QPainter.SmoothPixmapTransform, True)
        painter.setPen(Qt.NoPen)

        # 变换圆心
        painter.translate(self.rect().center())

        # 画背景方格图
        painter.save()
        # 保证方格在前景圆内部
        diameter = min(self.width(), self.height()) - 8
        radius = diameter / 2
        path = QPainterPath()
        path.addRoundedRect(-radius, -radius, diameter,
                            diameter, radius, radius)
        painter.setClipPath(path)

        pixSize = 5
        for x in range(int(self.width() / pixSize)):
            for y in range(int(self.height() / pixSize)):
                _x, _y = x * pixSize, y * pixSize
                painter.fillRect(_x - radius, _y - radius, pixSize, pixSize,
                                 Qt.white if x % 2 != y % 2 else Qt.darkGray)
        painter.restore()

        # 画前景颜色
        diameter = min(self.width(), self.height()) - 4
        radius = diameter / 2
        path = QPainterPath()
        path.addRoundedRect(-radius, -radius, diameter,
                            diameter, radius, radius)
        painter.setClipPath(path)

        painter.setBrush(self._color)
        painter.drawRoundedRect(-radius, -radius,
                                diameter, diameter, radius, radius) 
Example #8
Source File: image_dialog.py    From CvStudio with MIT License 5 votes vote down vote up
def __init__(self, parent=None):
        super(ImageDialogViewer, self).__init__(parent)
        self.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform)
        # self.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop)
        self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
        self.setDragMode(QGraphicsView.ScrollHandDrag)
        self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self._scene = QGraphicsScene(self)
        self.setScene(self._scene)
        self._pixmap = None 
Example #9
Source File: NinePatch.py    From PyQt with GNU General Public License v3.0 5 votes vote down vote up
def paintEvent(self, event):
        super(Label, self).paintEvent(event)
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setRenderHint(QPainter.SmoothPixmapTransform)
        try:
            self.image.SetImageSize(self.width(), self.height())
            self.image.Draw(painter, 0, 0)
        except Exception as e:
            print(e) 
Example #10
Source File: CircleImage.py    From PyQt with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, *args, antialiasing=True, **kwargs):
        super(Label, self).__init__(*args, **kwargs)
        self.Antialiasing = antialiasing
        self.setMaximumSize(200, 200)
        self.setMinimumSize(200, 200)
        self.radius = 100

        #####################核心实现#########################
        self.target = QPixmap(self.size())  # 大小和控件一样
        self.target.fill(Qt.transparent)  # 填充背景为透明

        p = QPixmap("Data/Images/head.jpg").scaled(  # 加载图片并缩放和控件一样大
            200, 200, Qt.KeepAspectRatioByExpanding, Qt.SmoothTransformation)

        painter = QPainter(self.target)
        if self.Antialiasing:
            # 抗锯齿
            painter.setRenderHint(QPainter.Antialiasing, True)
            painter.setRenderHint(QPainter.HighQualityAntialiasing, True)
            painter.setRenderHint(QPainter.SmoothPixmapTransform, True)

        #         painter.setPen(# 测试圆圈
        #             QPen(Qt.red, 5, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
        path = QPainterPath()
        path.addRoundedRect(
            0, 0, self.width(), self.height(), self.radius, self.radius)
        # **** 切割为圆形 ****#
        painter.setClipPath(path)
        #         painter.drawPath(path)  # 测试圆圈

        painter.drawPixmap(0, 0, p)
        self.setPixmap(self.target)
        #####################核心实现######################### 
Example #11
Source File: CircleLine.py    From PyQt with GNU General Public License v3.0 5 votes vote down vote up
def paintEvent(self, event):
        super(CircleLineWindow, self).paintEvent(event)
        if not self._canDraw:
            return
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setRenderHint(QPainter.SmoothPixmapTransform)
        self.draw(painter) 
Example #12
Source File: ScreenShotPage.py    From PyQt with GNU General Public License v3.0 5 votes vote down vote up
def onScreenShot1(self):
        # 截图方式1
        page = self.webView.page()
        frame = page.mainFrame()
        size = frame.contentsSize()
        image = QImage(size, QImage.Format_ARGB32_Premultiplied)
        image.fill(Qt.transparent)

        painter = QPainter()
        painter.begin(image)
        painter.setRenderHint(QPainter.Antialiasing, True)
        painter.setRenderHint(QPainter.TextAntialiasing, True)
        painter.setRenderHint(QPainter.SmoothPixmapTransform, True)

        # 记录旧大小
        oldSize = page.viewportSize()
        # *****重点就是这里******
        page.setViewportSize(size)
        frame.render(painter)
        painter.end()

        # 截图完成后需要还原,否则界面不响应鼠标等
        page.setViewportSize(oldSize)

        # 添加到左侧list中
        item = QListWidgetItem(self.widgetRight)
        image = QPixmap.fromImage(image)
        item.setIcon(QIcon(image))
        item.setData(Qt.UserRole + 1, image) 
Example #13
Source File: right_panel.py    From FeelUOwn with GNU General Public License v3.0 4 votes vote down vote up
def paintEvent(self, e):
        """
        draw pixmap as a the background with a dark overlay

        HELP: currently, this cost much CPU
        """
        painter = QPainter(self)
        painter.setPen(Qt.NoPen)
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setRenderHint(QPainter.SmoothPixmapTransform)

        # calculate available size
        draw_width = self.width()
        draw_height = 10  # spacing defined in table container
        draw_height += self.bottom_panel.height()
        if self.table_container.meta_widget.isVisible():
            draw_height += self.table_container.meta_widget.height()
        extra = self.table_container.current_extra
        if extra is not None and extra.isVisible():
            draw_height += extra.height()
        if self.table_container.toolbar.isVisible():
            draw_height += self.table_container.toolbar.height()

        scrolled = self.scrollarea.verticalScrollBar().value()
        max_scroll_height = draw_height - self.bottom_panel.height()

        if scrolled >= max_scroll_height:
            painter.save()
            painter.setBrush(self.palette().brush(QPalette.Window))
            painter.drawRect(self.bottom_panel.rect())
            painter.restore()
            return

        if self._pixmap is not None:
            self._draw_pixmap(painter, draw_width, draw_height, scrolled)
            self._draw_pixmap_overlay(painter, draw_width, draw_height, scrolled)
        else:
            self._draw_overlay(painter, draw_width, draw_height, scrolled)
            if scrolled >= 30:
                painter.save()
                painter.setBrush(self.palette().brush(QPalette.Window))
                painter.drawRect(self.bottom_panel.rect())
                painter.restore()
                return
        painter.end() 
Example #14
Source File: FlipWidget.py    From PyQt with GNU General Public License v3.0 4 votes vote down vote up
def paintEvent(self, event):
        super(FlipWidget, self).paintEvent(event)

        if hasattr(self, 'image1') and hasattr(self, 'image2') and self.isVisible():

            painter = QPainter(self)
            painter.setRenderHint(QPainter.Antialiasing, True)
            painter.setRenderHint(QPainter.SmoothPixmapTransform, True)

            # 变换
            transform = QTransform()
            # 把圆心设置为矩形中心
            transform.translate(self.width() / 2, self.height() / 2)

            if self._angle >= -90 and self._angle <= 90:
                # 当翻转角度在90范围内显示第一张图,且从大图缩放到小图的过程
                painter.save()
                # 设置翻转角度
                transform.rotate(self._angle, Qt.YAxis)
                painter.setTransform(transform)
                # 缩放图片高度
                width = self.image1.width() / 2
                height = int(self.image1.height() *
                             (1 - abs(self._angle / self.Scale) / 100))
                image = self.image1.scaled(
                    self.image1.width(), height,
                    Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
                painter.drawPixmap(
                    QPointF(-width, -height / 2), image)
                painter.restore()
            else:
                # 当翻转角度在90范围内显示第二张图,且从小图缩放到原图的过程
                painter.save()
                if self._angle > 0:
                    angle = 180 + self._angle
                else:
                    angle = self._angle - 180
                # 设置翻转角度, 注意这里角度有差异
                transform.rotate(angle, Qt.YAxis)
                painter.setTransform(transform)
                # 缩放图片高度
                width = self.image2.width() / 2
                height = int(self.image2.height() *
                             (1 - ((360 - abs(angle)) / self.Scale / 100)))
                image = self.image2.scaled(
                    self.image2.width(), height,
                    Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
                painter.drawPixmap(
                    QPointF(-width, -height / 2), image)
                painter.restore()