Help with Loops in Pixel Editing Sketch
in
Programming Questions
•
1 year ago
I have a sketch that I made based on some code that I found here a while ago. I think this question has a simple answer, bear with me as I'm not great at programming.
PImage img;
void setup() {
img = loadImage("image.jpg");
size(img.width,img.height);
}
void draw()
{
frameRate(30);
loadPixels();
for (int y = 0; y<height; y+=1 ) {
for (int x = 0; x<width; x+=1) {
int loc = x + y*img.width;
float r = red (img.pixels[loc]);
float g = green (img.pixels[loc]);
float b = blue (img.pixels[loc]);
float av = ((r+g+b)/3.0);
pushMatrix();
translate(x,y);
stroke(r,g,b);
strokeWeight(6);
if (r > 100 && r < 255) {
float h = random(-50, -15);
line(0,0,(av-255)/h,0-h);
}
popMatrix();
}
}
}
What I've been doing is manually changing the line with if (r > 100 && r < 255) { and changing the 100 and 255 to smaller ranges, outputting images at the different ranges and making animated GIFs. What I'd like to do is automate this. I tried adding in a for loop, and changing it to
if (r > r1 && r < r2) {
and then having r1+=15 and r2+=15 at the end of the loop... it just doesn't want to run however. I suspect it's the placement of the loop around the loadPixels? I'm not really sure, any guidance would be greatly appreciated!
PImage img;
void setup() {
img = loadImage("image.jpg");
size(img.width,img.height);
}
void draw()
{
frameRate(30);
loadPixels();
for (int y = 0; y<height; y+=1 ) {
for (int x = 0; x<width; x+=1) {
int loc = x + y*img.width;
float r = red (img.pixels[loc]);
float g = green (img.pixels[loc]);
float b = blue (img.pixels[loc]);
float av = ((r+g+b)/3.0);
pushMatrix();
translate(x,y);
stroke(r,g,b);
strokeWeight(6);
if (r > 100 && r < 255) {
float h = random(-50, -15);
line(0,0,(av-255)/h,0-h);
}
popMatrix();
}
}
}
What I've been doing is manually changing the line with if (r > 100 && r < 255) { and changing the 100 and 255 to smaller ranges, outputting images at the different ranges and making animated GIFs. What I'd like to do is automate this. I tried adding in a for loop, and changing it to
if (r > r1 && r < r2) {
and then having r1+=15 and r2+=15 at the end of the loop... it just doesn't want to run however. I suspect it's the placement of the loop around the loadPixels? I'm not really sure, any guidance would be greatly appreciated!
1