Change a black pixel into a blue one
-
Hi guys,
I've wrote this piece of code that should make the black part of an image into a blue part. But the code doesn't change the black into the blue it never comes in the if test I think.
Could someone help me?
@ QImage background;
QImage world(1500, 768, QImage::Format_RGB32);
QSize sizeImage;
int height, width;
background.load("Background.png");
world.fill(1);QPainter painter(&world); sizeImage = background.size(); width = sizeImage.width(); height = sizeImage.height(); const QRgb black = 0; const QRgb blue = 255; for(int y = 0; y < height; y++) { for(int x = 0; x < width; x++) { if (background.pixel(x,y) == black) { background.setPixel(x,y,blue); } } } painter.drawImage(0,0,background); //adding new image to the graphicsScene QGraphicsPixmapItem item( QPixmap::fromImage(background)); QGraphicsScene* scene = new QGraphicsScene; scene->addItem(&item); QGraphicsView view(scene); view.show();@
Kind regards,
-
Qt has "predefined color values":http://developer.qt.nokia.com/doc/qt-4.8/qt.html#GlobalColor-enum
Those might be easier to use.
Once again the question: are you sure that your black pixels are absolutely black as you have defined? The values might be only almost black and then your if statement will not work. -
The are 000000 hex value in illustrator so I think they are black not?
-
If they have an alpha channel, then it would be added in (probably as 255 for completely opaque.)
You may want to iterate your image, and more closely examine the resultant QRgb returned by pixel() to check the r,g,b values rather than assuming a numeric constant.
-
Thanks mlong, that helped the if test works now, but the repainting not, this my code can you please help me?
@ QImage background;
QImage world(1500, 768, QImage::Format_RGB32);
QSize sizeImage;
int r,g,b,alpha;
QColor color;
int height, width;
background.load("Background.png");
world.fill(1);QPainter painter(&world); sizeImage = background.size(); width = sizeImage.width(); height = sizeImage.height(); const QRgb black = 0; const QRgb blue = 255; for(int y = 0; y < height; y++) { for(int x = 0; x < width; x++) { color = background.pixel(x,y); color.getRgb(&r,&g,&b,&alpha); if (r == 0 && g == 0 && b == 0) { cout << "Gelukt"; background.setPixel(x,y,Qt::blue); } } } painter.drawImage(0,0,background); //adding new image to the graphicsScene QGraphicsPixmapItem item( QPixmap::fromImage(world)); QGraphicsScene* scene = new QGraphicsScene; scene->addItem(&item); QGraphicsView view(scene); view.show();@
-
You're confusing QColor with QRgb.
Try something like:
@
...
QRgb color; // Not QColor
...
color = background.pixel(x,y);
if (qRed(color) == 0 && qGreen(color) == 0 && qBlue(color) == 0) {
background.setPixel(x,y,QColor(Qt::blue).rgb()); // Qt::blue is a QColor constant.
}
...
@Brain to terminal. Just an example. YMMV
-
Thank you so much !! :-)