简体   繁体   中英

Manipulate color of QImage - QT

I have one generic icon image, which has an alpha. Lets say a black sphere placed on an square button, with transparancy. Now I would like to change the color of the icon on the fly, without having several image of sphere_black.png, sphere_red.png etc etc. Is there a way to colorize the pixmap, respecting the alpha and change HSV on that pixel, for all in the map?

I have something like this, but stuck:

img = QtGui.QImage(kwargs['icon_path']
pxmap = QtGui.QPixmap(img)
for x in range(img.width()):
    for y in range(img.height()):
         print img.pixel(1, 1), '###'
         # ???? #

Any help is appreciated!

QGraphicsColorizeEffect might be what you are looking for. Sadly the QGraphicsEffect class is made to be used with the graphics view framework, it can't easily be applied to a QImage . However there are workarounds for that, as this discussion shows.

The implementation of the effect in QPixmapColorizeFilter::draw() shows how the colourization is done: A coloured rect (with the color having the alpha set to something else than fully opaque) is drawn over the image with QPainter::fillRect() , with an appropriate composition mode set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM