亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 QPainter 用 alpha 值覆蓋兩個像素圖

使用 QPainter 用 alpha 值覆蓋兩個像素圖

慕婉清6462132 2021-08-24 18:13:45
我正在嘗試覆蓋 2 個像素圖并將它們轉換為 QGraphics 場景中的單個像素圖。兩個像素圖在某些位置都是透明的。我想使用此處列出的“SourceOver”混合類型組合貼圖:我在下面有一個簡單的玩具示例來說明我創建了兩個虛擬透明像素圖,一個綠色和一個藍色的問題。實際上,這些地圖是從圖像加載并繪制的,但是這個例子重現了這個問題?;诖巳绾卧诹硪粋€圖像的頂部添加圖像?,我嘗試的方法(注釋掉 4 行)是用其中一個像素圖創建一個 QPainter,然后在它上面繪制另一個像素圖,但是這會導致程序崩潰。關于如何解決這個問題的任何想法?我最終希望能夠保存組合的像素圖。import sysfrom PyQt5 import QtCore, QtGui, QtWidgetsfrom PyQt5.QtGui import QPixmap, QPainter, QPen, QBrush, QPainterPathfrom PyQt5.QtCore import (QLineF, QPointF, QRectF, Qt)class Viewer(QtWidgets.QGraphicsView):    def __init__(self, parent):        super(Viewer, self).__init__(parent)        self._scene = QtWidgets.QGraphicsScene(self)        self.photo = QtWidgets.QGraphicsPixmapItem()        self.label = QtWidgets.QGraphicsPixmapItem()        self._scene.addItem(self.photo)        self._scene.addItem(self.label)        self.setScene(self._scene)            def overlayMaps(self):        blue = QtGui.QPixmap(600, 600)         blue.fill(QtGui.QColor(0,0,255,0))        p = QPainter(blue)        self.pen = QPen()        self.pen.setColor(QtGui.QColor(0,0,255,255))        self.pen.setWidth(10)        p.setPen(self.pen)        p.drawLine(0,0,600,600)        green = QtGui.QPixmap(600, 600)        green.fill(QtGui.QColor(0,255,0,0))                    p = QPainter(green)        self.pen = QPen()        self.pen.setColor(QtGui.QColor(0,255,0,255))        self.pen.setWidth(10)        p.setPen(self.pen)        p.drawLine(600,0,0,600)        self.photo.setPixmap(blue)        self.label.setPixmap(green)         resultPixmap = QtGui.QPixmap(self.photo.pixmap().width(), self.photo.pixmap().height())#        resultPainter = QtGui.QPainter(resultPixmap)#        resultPainter.setCompositionMode(QtGui.QPainter.CompositionMode_SourceOver)#        resultPainter.drawPixmap(300,300, self.photo.pixmap()) #        resultPainter.drawPixmap(300,300, self.label.pixmap()) 
查看完整描述

1 回答

?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

我已經實現了一個根據模式執行加入動作的功能,為了更好地欣賞我已經移動了項目。


import sys

from PyQt5 import QtCore, QtGui, QtWidgets


def join_pixmap(p1, p2, mode=QtGui.QPainter.CompositionMode_SourceOver):

    s = p1.size().expandedTo(p2.size())

    result =  QtGui.QPixmap(s)

    result.fill(QtCore.Qt.transparent)

    painter = QtGui.QPainter(result)

    painter.setRenderHint(QtGui.QPainter.Antialiasing)

    painter.drawPixmap(QtCore.QPoint(), p1)

    painter.setCompositionMode(mode)

    painter.drawPixmap(result.rect(), p2, p2.rect())

    painter.end()

    return result


class Viewer(QtWidgets.QGraphicsView):

    def __init__(self, parent=None):

        super(Viewer, self).__init__(parent)

        self._scene = QtWidgets.QGraphicsScene(self)

        self.setScene(self._scene)  


        blue = QtGui.QPixmap(100, 100) 

        blue.fill(QtCore.Qt.transparent)

        p = QtGui.QPainter(blue)

        pen = QtGui.QPen(QtGui.QBrush(QtGui.QColor(0,0,255)), 10)

        p.setPen(pen)

        p.drawLine(0, 0, 100, 100)

        p.end()

        self.photo = self._scene.addPixmap(blue)


        green = QtGui.QPixmap(100, 100)

        green.fill(QtCore.Qt.transparent)            

        p = QtGui.QPainter(green)

        pen = QtGui.QPen(QtGui.QBrush(QtGui.QColor(0, 255, 0, 255)), 10)

        p.setPen(pen)

        p.drawLine(100, 0, 0, 100)

        p.end()

        self.label = self._scene.addPixmap(green) 

        self.label.setPos(200, 0)     


        self.overlayMaps()


    def overlayMaps(self):

        p1 = QtGui.QPixmap(self.photo.pixmap())

        p2 = QtGui.QPixmap(self.label.pixmap())


        result_pixmap = join_pixmap(self.photo.pixmap(), self.label.pixmap())

        self.result_item = self._scene.addPixmap(result_pixmap)

        self.result_item.setPos(100, 200)


        result_pixmap.save("result_pixmap.png")


if __name__ == '__main__':

    import sys

    app = QtWidgets.QApplication(sys.argv)

    window = Viewer()

    window.resize(640, 480)

    window.show()

    sys.exit(app.exec_())

http://img1.sycdn.imooc.com//6124c67c00010cfc06500527.jpg

result_pixmap.png

http://img1.sycdn.imooc.com//6124c6860001e21201180125.jpg


查看完整回答
反對 回復 2021-08-24
  • 1 回答
  • 0 關注
  • 551 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號