We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, Here is the example of a png image. I want to change only black colored part of png and transparent part should remain untouched. I have tried so many things but I was unsuccessful.
Please download the image for trying
Here is the code that I have tried
PImage img;
color c;
void setup() {
size(500, 500);
img = loadImage("handOpen.png");
}
void draw() {
background(-1);
img.loadPixels();
for (int x = 0; x < img.width; x++) {
for (int y = 0; y < img.width; y++) {
int i = x+ y*img.width;
if (img.get(x, y)==0) {
img.pixels[i] = c;
}
}
}
img.updatePixels();
image(img, width/2, height/2);
}
void mousePressed()
{
c = (color) random(#000000);
}
Answers
The code below should work
Thanks quark! but it is not working though I am able to see the outline of the hand which is in black color and rest of the pixels are white inside the outline.
What I wanted is that initially image should show the original image (black hand) and as I click it should change the color.
anti-aliasing means that these pixels aren't dead black or wholy opaque, the above code doesn't handle that.
also, no need to change the pixels in every draw loop when the colour only changes in the mousePressed()
and one of your img.width should be a .height.
@koog thank you ! but still no luck. I have this code where I put all the image color change in mousePressed event. Also color only change once not every mouse click even if I put img.updatePixel(); Any suggestion.
the next time through the pixels aren't black, they are whatever colour you changed them to last. so the == #000000 won't work.
perhaps easiest would be to make the hand white on transparent, then use tint(). see the reference.
I have almost scrap the idea of using PNG it may seems difficult to use so I have decided to use SVG files but still no luck .I have converted this image into svg using Illustator CS3 and whenever I call svg shape in processing I got this error
Ignoring < clipPath > tag.
It shows nothing but a black rectangle I have no idea where I am getting it wrong.
I have solved this problem .. but I have one question about performance.
Question : So can you tell me if using image is fast or PShapes svg files? In both cases I have re-sized their size almost 1/8 of original of size.
this works with a white hand:
Thanks a lot koogs ! It Worked.
Can you also tell me what would be better to use as performance wise, an Image or SVG ?
blyk, you posted a number of messages, this one was still in the wrong section, it might be time to get a look at the Categories descriptions, no?
Sorry PhiLho but I didn't see it as wrong section because I have posted my sample code and I am asking to fix it (as mention in the categories section)
It is in the Question about Code because I moved it! It was in the default category, Using Processing, which about usage of the PDE. If that's just distraction on only this topic, please ignore my remark and be more attentive. ;-)
thanks you so much PhiLho :)