We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Looping through pixels in image
Page Index Toggle Pages: 1
Looping through pixels in image (Read 1367 times)
Looping through pixels in image
May 4th, 2010, 8:07am
 
I am just beginning with processing and a bit lost. I'd appreciate any help with the below. Thanks.

I want to load an image into Processing and loop through each pixel in the image to retrieve the colour of that specific pixel coordinate. Using this information I then want to display each pixel colour to fill the screen, one after the other so it is basically just a succession of individual colours from the original image being displayed on screen as the end result.

I've managed to look at some of the examples for working with pixels and images, but I cannot work out how to achieve this and where to start with the code.

Thank you for any advice.


Re: Looping through pixels in image
Reply #1 - May 4th, 2010, 8:33am
 
PImage | pixels[] would seem a good place to start.  You might want to read the documentation for get(), and other related methods, closely too...

If you don't understand the reference then be specific about what you can't figure out and preferably post some code...
Re: Looping through pixels in image
Reply #2 - May 4th, 2010, 8:48am
 
As blindfish said.
You might want to change the frameRate so user can see each frame, and you can just use background() with the color you got from pixels[] to achieve your goal.
Re: Looping through pixels in image
Reply #3 - May 4th, 2010, 12:50pm
 
Thank you for your replies. I have now got the code to get one pixel from the image and display this as the background color. However, I can't seem to work out how I would loop through the pixels in the image? I'm sure it must be pretty straightforward.
Here is the code so far:

PImage pic;

void setup(){
 size(400,400); //size
 pic = loadImage("image01.jpeg");
 noLoop();
}

void draw(){
 image(pic, 0, 0);
 pic.loadPixels();
 for (int i = 0; i < (width*height); i++){
   color cp = get(200, 100);
   background(cp);
 }
 pic.updatePixels();

}

Thank you for any further advice.
Re: Looping through pixels in image
Reply #4 - May 4th, 2010, 1:15pm
 
You will need to move through the pixels and set the background colour on different draw cycles or the background colours won't be displayed.

Something like this should achieve what you are after:

Code:

PImage pic;
int i = 0, j= 0;

void setup()
{
size(400,400); //size
pic = loadImage("image.jpg");
}

void draw()
{
 //at end of row move to first pixel of next row
 if (i == width)
  {
    i = 0;
    j++;
  }

  //at end of picture go back to start
  if (j == height)
    j=0;
 
  //Set background to current pixel colour
  background(pic.get(i,j));
 
  //Move to next pixel
  i++;
}
Re: Looping through pixels in image
Reply #5 - May 5th, 2010, 4:49am
 
Thanks! That's exactly what I wanted.  Cheesy
Re: Looping through pixels in image
Reply #6 - May 5th, 2010, 5:15am
 
Another way to do it:
Code:
PImage pic;
int count = 0;

void setup(){
size(400,400); //size
pic = loadImage("D:/Dev/PhiLhoSoft/images/TestImageS.jpg");
pic.loadPixels();
image(pic, 0, 0);
frameRate(10);
}

void draw(){
color cp = pic.pixels[count++];
count %= pic.pixels.length; // Reset to 0 at end of image
background(cp);
}
Re: Looping through pixels in image
Reply #7 - May 5th, 2010, 7:10am
 
They are both really useful. Thank you all for your help and advice.

One bit I'm not sure about is

count %= pic.pixels.length;

how does this part work?
Re: Looping through pixels in image
Reply #8 - May 5th, 2010, 8:21am
 
See % (modulo)
Basically, it resets the counter to zero when exceeding the given value.

%= like += or *= and other <operator>= performs the operation between the left part and the right part then assign the result to the left variable.
Page Index Toggle Pages: 1