We are about to switch to a new forum software. Until then we have removed the registration on this forum.
hello! I have PNG with transparency, green, and white I will like to be able to change the green to another colour and the white for another.
I imagine it will go something like this
PImage myImage = loadImage("apples.jpg");
image(myImage, 0, 0);
loadPixels();
get () green pixel
change green pixel to x pixel
get () white pixel
change white pixel to y pixel
updatePixels();
I am a bit confuse on how to proceed between loadPixels(); and updatePixels(); if it the right way, any help will be appreciated.
Answers
You need to use a loop to look at each pixel in your image in turn. If you're looking at a pixel and it's green, store a different color in that pixel instead. Or if the pixel you are looking at is white, store a different color in that pixel.
This process translates almost directly into code.
I'll leave changing the white ones up to you.
Cool image. :))
Already knew that pic was from your own site. This is an old sketch after all: :))
http://forum.Processing.org/two/discussion/11880/how-can-i-speed-up-this-basic-image-processing-code
Thanks! TFguy44 the code work good, but it is missing some pixel. I have changed the original colour to black
colour (00, 00, 00)
I imagine that some pixel on the edge is not exactly the same number. Is there an easy way to get all the colour near black, it is the same thing with the white? I imagine that if I have a better contrast in colour it will be easy to change for others.sounds like a job for dist()...
Koogs, I have looked at dist() function but I do not think it is the good function. When I said all the colour near black I do not tall near in terms of distance but near in terms of colour. how to change the pixel that is not pure black (0,0,0)
hope it better to understand.
If you consider the red, green and blue components of a color as coordinates in a 3d-space, you could use dist to calculate the distance betwen colors.
and if that distance is below a certain treshold, you can exchange the color.
OK!!! :)
Benja, I am not able to find a way to get.
I feel confused on how to use the number I get. 8-}
You want to include a colour if its distance is within a certain threshold. So just
will do.
Hint: don't start variables with upper case letters, you'll just confuse people. Go read the Java naming conventions.
But you need to calculate the distance for every pixel so it needs to be inside the loop.
I still need help. I imagine it needs to be added here. loadPixels(); for ( int i=0; i<pixels.length; i++) { // Look at each pixel. if ( pixels[i] == color(col1)) { // If it's color 1 or near pixels[i] = color(imgChCol1); // turn it into red. } } updatePixels();
How do I mix
col1
withif (dcolB < threshold)
do I need to makeIf () { if()}}
if inside an if Or I create an If inside col1Thanks to let me know about Java naming conventions, I will have a look..
Colors that are similar have about the same amount of red, green, and blue in them.
Here is a red color: color(255,0,0) Here is an almost red color: color(200,30,30) Here is a black color: color(0,0,0)
How "far apart" from each other are these colors? If you treat the red, green, and blue values like x, y, and z co-ordinates, your colors can be treated like points in space. And you can determine how far apart they are using dist():
The red and almost red colors are fairly "close". The red and black colors are not. By using a threshold and a conditional statement, you can do certain code for colors that are close to a given color.
You can get the red, green, and blue values from a color by using red(), green(), and blue(). Look again at benja's 4th line of code.
if i do this int col1 = color(0, 0, 0); int col2 = color(10, 10, 10); float dcolB = dist(red(col1), green(col1), blue(col1), red(col2), green(col2), blue(col2));
That print the number 17.320509 Thit number is the distance between the color 1 and 2 or it convert to a color like this
float dcolB = color(dist(red(col1), green(col1), blue(col1), red(col2), green(col2), blue(col2)));
here, screenshot to let you see the problem
and also a small problem. i get this on the screen, but if i use saveFrame(); it do not save the color change
Can you format your code please. Highlight it, hit ctrl-o.
if i do this
That print the number 17.320509. Thit number is the distance between the color 1 and 2. Or i can convert to a color like this
float dcolB = color(dist(red(col1), green(col1), blue(col1), red(col2), green(col2), blue(col2)));
No, the example was only to show how you could find the distance between two colors. You can use that value (i.e. 17) as your threshold.
What you want to do is test all pixels from an image against your color (i.e. white) and replace it with a second color, if the distance between your first color and the current pixel-color is lower than your desired threshold.
Thanks Benja, it works. I don’t think I will have been able to figure all by myself but when I see it, I understand it.
But this code change brings other problems. Is it better if continue here on this tread or make a new one for a new problem?
now I try to mix the benja code with https://forum.processing.org/one/topic/combine-two-pimages-into-one.html
But the color change do not apply. Maybe it will be better to understand if you have the complete code?
i != I, variables are case sensitive
and how are you saving the image? because that snippet only updates the offscreen PImage, 'output', not the visible image.