0

I want to transparent image by mouse event. I try rectangle on the image, but it fills with color black with Xor_mode and remain red rectangle with DestinationIn_mode.

My goal is to erase some pixels by mouse then blends multiple images together.

Someone can help me?

img

with the code below I get this ,but why BLACK but not the transparent background with striped lines?

import sys

from PyQt5.QtCore import QRectF, Qt, QPoint, QPointF, QRect
from PyQt5.QtGui import QPen, QBrush, QPixmap, QPainter, QImage, QPainterPath, QColor, QBitmap
from PyQt5.QtWidgets import QApplication, QWidget, QGraphicsView, QGraphicsScene, QFrame, QGraphicsPixmapItem, \
    QGraphicsItem, QStyleOptionGraphicsItem, QGraphicsSceneMouseEvent, QGraphicsRectItem


class CutImg(QGraphicsItem):
    def __init__(self, qPixmap):
        super(CutImg, self).__init__()
        self.qPixmap = qPixmap

    def boundingRect(self):
        return QRectF(self.qPixmap.rect())

    def paint(self, qPainter, qStyleOptionGraphicsItem, widget=None):
        qPainter.drawPixmap(QPointF(), self.qPixmap)
        qPainter.setCompositionMode(QPainter.CompositionMode_Xor)
        qPainter.fillRect(QRectF(QPointF(30, 50), QPointF(100, 150)), QBrush(Qt.red))



class CutScene(QGraphicsScene):
    def __init__(self):
        super(CutScene, self).__init__()
        self.cuttedImg = None

    def setPixmap(self, qPixmap):
        self.mainImg = CutImg(qPixmap)
        self.addItem(self.mainImg)
        self.cuttedImg = qPixmap


class MainCutWindow(QGraphicsView):
    def __init__(self):
        super(MainCutWindow, self).__init__()
        self.scene = CutScene()
        self.setScene(self.scene)
        self.boardmargin = 100

    def setPixmap(self, qPixmap):
        self.scene = CutScene()
        self.setScene(self.scene)
        self.setFixedSize(qPixmap.width() + self.boardmargin * 2, qPixmap.height() + self.boardmargin * 2)
        self.scene.setPixmap(qPixmap)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainCutWindow()
    window.setPixmap(QPixmap('images/cat.jpg'))
    window.show()
    app.exec_()
Jay Zhan
  • 307
  • 3
  • 6
  • I don't understand you, could you explain me better. You can also provide the .jpg – eyllanesc Dec 22 '19 at 03:57
  • the goal I want to do is like the first image, draw somethings by mouse and transparent the image, as "erase" the image, but I get the black color instead, how to fix it? – Jay Zhan Dec 22 '19 at 04:15

0 Answers0