Pretty simple, I want to create subtle variation in position in the X and Y axis using noise() on a PImage. I’ve messed around with the pull and pop matrix with no luck. Any help is appreciated.
void setup() {
}
void draw() {
background(200,200,0);
loadPixels();
int line = height/2;
for ( int i = width*line; i< width*(line+1);i++) pixels[i]=color(0,0,200); // make a line at line
//_____________________________________________________now inhere can draw what you want
rect (20,20,20,20);
//_____________________________________________________but when you do
updatePixels();
//_____________________________________________________it's gone
}
This was unclear to me. Did you mean that you want to:
move the cells – drift each pixel of the image in a random direction – so that it is “scattered”, like powder, and some parts display outside the bounding rectangle, and some overlap, and some have no image data at all in them.
or
sample into the cells – populate each pixel of the image from a random nearby pixel – but still end up with a grid of color in a rectangular bounding box, and each has a color.
This was the aim, yes. I’m hoping to do this with the pushMatrix + translate. I’ve simplified my code as a basis to start with but even that isn’t working:
PImage source;
int loc;
color c;
float h;
float s;
float b;
int x;
int y;
void setup(){
colorMode(HSB);
source = loadImage("source.jpg");
size(source.width,source.height);
background(0);
source.loadPixels();
int loc = y*source.width+x; // Map pixel array to cordernates in 2d space
color c = (source.pixels[loc]); //Assigns c color to pixel array
float h = hue(source.pixels[loc]);
float s = saturation(source.pixels[loc]);
float b = brightness(source.pixels[loc]);
source.updatePixels();
}
void draw(){
source.loadPixels();
float scale = 0.01;
for(int x = 0; x<width;x++){
for(int y = 0; y<height;y++){
ellipse(x,y,10,10);
fill(h,s,(b*noise(scale*x,scale*y)));
}
}
source.updatePixels();
}
I don’t recommend pushing and popping the matrix for every pixel – that is going to be really, really inefficient. I wouldn’t even use PImage.get(). Instead, I would recommend a for loop iterating over jpg.pixels.
Getting the color of a single pixel with get(x, y) is easy, but not as fast as grabbing the data directly from pixels[]. The equivalent statement to get(x, y) using pixels[] is pixels[y*width+x] . See the reference for pixels[] for more information.
Do you want your pixels to be jumping around every frame, or do you want the scattered values to be stable / static? If static you could pre-generate a list of 2*pixels noise values and store them in 1 or 2 arrays, for your x and y offsets.