We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi,
I want to search an image for a color and replace that pixel with another color.
This is my test code so far.
function setup() { img = loadImage("../test5.jpg"); var c = createCanvas(600, 600); background(250); } function draw() { mask(); image(img,0,0); //noLoop(); } function mask() { var tempMe; img.loadPixels(); //print("wow"); for (var i = 0; i < img.width; i++) { for (var j = 0; j < img.height; j++) { tempMe = img.get(i,j); if ( tempMe[0] == 255 ) { img.set(i, j, color(100, 50, 100)); } } } img.updatePixels(); }
It runs crazy crazy slow even with small images. Any ideas? cheers
Answers
Dunno. This is fast.
Alternative version based on "SetAllColorAlpha II" : :D
http://forum.Processing.org/two/discussion/10291/issue-with-color-alpha
Avoid using the get() and set() methods because they are SLOW.
Instead use the pixel array as shown by TfGuy44. This version is slightly quicker because it does not use the double loop and the repeated calls to
color(...)
.Sorry. My bad with the double loop. I was tired last night. :-/
This is the exact code I have now:
Just modified a few things above. But now the output is:
swapped 0 swapped 1 swapped 2 swapped 3
it thinks pixel length is 4 :(
then it just loads the original unchanged image.... using the test image you linked to now..
I believe this is because my server takes a second to load the image. If I put the call to pixel_swap() in the draw function it will, after a couple of draw rounds, get the proper size of the image. It still doesn't replace the pixels in the image though it will draw the image to the canvas.
if I print img.pixels[i] it just shows undefined and not as a p5.color
Maybe this is a processing versus p5 thing?
Could it be because I don't have a type PImage? (p5 produces an error if I try to use a type beside var)
thanks!
Sorry my bad! I've failed to realize this is about p5.js framework. Following the other answerers! :-\"
And I've ended up pushing a "Java Mode" example for swapping! X_X
Indeed the way p5.js stores p5.Image's pixels[] is more complex.
Rather each index being 1
color
, we need to access a sequence of 4 in order to get a full RGBa value.Here's the same Java code adapted for p5.js. Any doubts about it just ask further: O:-)
thanks, almost there
I have never seen notation like outer: before so that statement kind of confuses me. Can you explain it a bit more?
Is there a way do this, but:
1) color something like every fifth red pixel? (creating a pattern / mask look) and also can I do this with:
2) multiple colors at once.
Like say find one yellow color and one red color and replace both with black.
thanks again!
It's not a notation but a label. It can be any name just like a variable.
It's used w/
continue
&break
keywords in order to choose which loop it needs to escape into.For further details, read
continue
(orbreak
)'s reference:https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/continue
https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label
Simply re-invokes swapPixelColor() w/ diff. old &/or now color parameters for the same p5.Image.
I can't pinpoint exactly what you mean by that!
Assuming you merely wanna change each 5th pixels[]'s color to something else, you just need to skip 5*4 indices.
That is awesome. Thanks so much.
Here is my final code for this in case anyone is looking in the future:
Congratz! And here are some extra advanced tips: >-)
http://p5js.org/reference/#/p5/color
rgbToHex(209, 27, 102)
, go w/[209, 27, 102]
. Or evencolor(209, 27, 102)
:ar!'#000000'
is considered gray scale, a simple0
does the work!'#FFFF00'
can be replaced w/ a more expressive'yellow'
! :-bdhttp://p5js.org/reference/#/p5.Image/get
swapped = swapPixelColor(swapped.get(), rgbToHex(245, 239, 129), '#000000');
swapPixelColor(swapped, [245, 239, 129], 0);