Qt / PyQt (/ Other?): How to change certain colors in pixmap?

How to change certain colors in pixmap? For example, I have a pixmap with white and black pixels, and I want to change all white pixels to blue, but leave only black ones. Or maybe change black to white and white to blue ... [I’m looking for a solution in Qt / PyQt, but maybe this is a general question about how to process / compose pixel files.]

+4
source share
1 answer

You can use createMaskFromColor to create a bitmap for white pixels, then use drawPixmap to overwrite them with a different color.

  pix = QPixmap("test.png") mask = pix.createMaskFromColor(QColor(255, 255, 255), Qt.MaskOutColor) p = QPainter(pix) p.setPen(QColor(0, 0, 255)) p.drawPixmap(pix.rect(), mask, mask.rect()) p.end() 

Note that createMaskFromColor converts pixmap to QImage , so you should try using QImage if possible.

+9
source

Source: https://habr.com/ru/post/1387927/


All Articles