简体   繁体   English

操纵QImage的颜色-QT

[英]Manipulate color of QImage - QT

I have one generic icon image, which has an alpha. 我有一个通用的图标图像,其中有一个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? 现在,我想即时更改图标的颜色,而没有sphere_black.png,sphere_red.png等的几张图像。有没有办法为像素图着色,尊重alpha并更改该像素上的HSV,全部在地图上?

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. QGraphicsColorizeEffect可能就是您想要的。 Sadly the QGraphicsEffect class is made to be used with the graphics view framework, it can't easily be applied to a QImage . 可悲的是, QGraphicsEffect类被设计为与图形视图框架一起使用,它不能轻易地应用于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. QPixmapColorizeFilter :: draw()中效果的实现显示了如何完成着色:使用QPainter::fillRect()在图像上绘制有色rect(其alpha设置为完全不透明的颜色QPainter::fillRect() ,设置适当的构图模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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