Make a still image from an array of pixels
in
Programming Questions
•
6 months ago
Hello to all,
I am new to processing. I want to do a project using the pixels of a still image but I am so confused! :)
First, here's what I want to do :
1- Load the image (quite easy!)
2- Store all its pixels in an array (ex: [0] 156, [1] 157, [2] 158 ...)
3- Inverse the array (ex: [0] 158, [1] 157, [2] 156 ...)
4- Display the result (and save it as a .tif file)
I based the code on examples from the processing website and the forum.
I also used a few println to see what was going on.
I think steps 2-3 were successful as the array is filled by a proper number of index and values (extremely weird values though since I was unable to convert them into rgb).
Step 4 is however a disaster... How may I assemble an image from an array of pixels?
I played with createImage but I failed... what am I missing?
Thanks for the help and ideas!
Here is the code:
PImage img;
color [] imageColors;
boolean test = false;
String fileName = "parr1_final.tif";
void setup() {
size(504, 360);
img = loadImage( "test1parr.jpg");
image(img,0,0);
img.loadPixels();
imageColors = new color[img.pixels.length];
for (int x=0; x < img.width; x++) { // search each line and row of the image
for (int y=0; y < img.height; y++) {
int loc = x + y*img.width;
float r = red (img.pixels [loc]);
float g = green (img.pixels [loc]);
float b = blue (img.pixels [loc]);
imageColors[loc] = color(r, g, b);
}
}
println(imageColors);
test = true;
if (test) {
//println("true");
for(int i = 0; i < imageColors.length/2; i ++) { // reverse the array
int temp = imageColors[i];
imageColors[i] = imageColors[imageColors.length - i - 1];
imageColors[imageColors.length - i - 1] = temp;
}
img.updatePixels();
println(imageColors);
img.save(fileName);
}
}
void draw() {
}
1