Change a black pixel to a blue pixel in an image
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?
Code:
int height, width;
background.load("Background.png");
world.fill(1);
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
scene->addItem(&item);
view.show();
Kind regards,
Re: Change a black pixel to a blue pixel in an image
have you define black and blue correctly? seems to be the problem if you are saying that it never enters the IF....try using Qt::black
Re: Change a black pixel to a blue pixel in an image
While the use of integers to initialise a QRgb looks odd it is correct language-wise. QRgb is a typedef equivalent to unsigned int.
It is not working as expected because setting the value this way has set the alpha channel to 0: the colour exists but is 100% transparent and will not match an opaque black/blue pixel. Try setting the colours using the qRgb() macro or, as KillGabio suggests, using symbolic names.