I’d like to rearrange a JPG’s pixels so that they were in random order - preferably in 4x4 blocks.
I’m having a little trouble figuring this out. I was hoping something like the following would work - but it doesn’t. I noticed that people are very good at identifying those sorts of problems here, so I’m posting again with the hope that maybe someone can help me head in the right direction.
PImage img;
size(700, 600);
loadPixels();
img = loadImage("cran.jpg");
// Loop through every pixel column
for (int x = 0; x < width; x++) {
// Loop through every pixel row
for (int y = 0; y < height; y++) {
// Use the formula to find the 1D location
int loc = x + y * width;
if (x % 12 == 0) { // If we are an even column
pixels[int(random(loc))] = pixels[int(random(loc))];
}
}}
updatePixels();
before all relevant commands (the commands must refer to the image, otherwise they just refer to the screen)
loadPixels()
width
height
pixels
updatePixels()
for bigger parts of the image use img.get()
Sketch
// test for img
PImage img;
size(700, 600);
img = loadImage("cran.png");
img.loadPixels(); // !!!!!!!!!!!!!!!
// Loop through every pixel column
for (int x = 0; x < img.width; x++) {
// Loop through every pixel row
for (int y = 0; y < img.height; y++) {
// Use the formula to find the 1D location
int loc = x + y * img.width;
if (x % 2 == 0) { // If we are an even column (2 or 12)
img.pixels[int(random(loc))] = img.pixels[int(random(loc))];
}
}
}//for
img.updatePixels();
image(img, 0, 0);