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 › How to get a PImage of the sketch
Page Index Toggle Pages: 1
How to get a PImage of the sketch? (Read 1350 times)
How to get a PImage of the sketch?
Jul 15th, 2009, 1:50am
 
Hi, I am not sure if the subject is correct, but here it goes, I have the following code working fine

Code:
PImage img;
Ripple ripple;

img = loadImage("water_test.jpg");
ripple = new Ripple(img);


I need to change it so the ripples class gets a PImage of the screen, so whatever is draw get pass as PImage to the ripple class, hope this makes sence

I am aware of loadPixels() and createImage() but I cant get it to work

Any ideas are welcome!

Cheers
rS
Re: How to get a PImage of the sketch?
Reply #1 - Jul 15th, 2009, 1:55am
 
See also get().
Re: How to get a PImage of the sketch?
Reply #2 - Jul 15th, 2009, 2:31am
 
Hi PhiLho, yes that did it

Code:
img.loadPixels();
 for (int y = 0; y < height; y++) {
   for (int x = 0; x < width; x++) {
     int px = get(x, y);
     img.pixels[x + y * width] = px;
   }
 }
img.updatePixels();


Got that into a fucntion, because in order to work it needs to be refresh in the draw method

If there is a simpler or better way, please post it

On another note what is the difference in using createImage or new PImage?

Code:
img = createImage(width, height, RGB);
img = new PImage(width, height);


The Reference say "To create a new image, use the createImage() function (do not use new PImage())."

I get the same results using either, but there must be something to it, where is the catch, anyone?

Thanks
rS
Re: How to get a PImage of the sketch?
Reply #3 - Jul 15th, 2009, 4:24am
 
nardove wrote on Jul 15th, 2009, 2:31am:
If there is a simpler or better way, please post it
Yes: use get() without parameters... Smiley

Quote:
On another note what is the difference in using createImage or new PImage
The main difference is that you can use one and should not use the other, as pointed out... Tongue

No, seriously, as you guessed it, there is a catch: createImage() does a new PImage(wide, high, format) then does image.parent = this which, "makes save() work without needing an absolute path".
So if you do new PImage, and do the above additional init, or don't use save(), it should be OK.
Note: you have actually several constructors:
PImage() sets format to ARGB but doesn't allocate pixels.
PImage(int width, int height) sets format to RGB.
PImage(int width, int height, int format) allows to choose the format.
PImage(java.awt.Image img) takes a Java image and set format to RGB.
Re: How to get a PImage of the sketch?
Reply #4 - Jul 15th, 2009, 4:37am
 
Thanks again, now why is it better to use get(), instead of get(x, y);

When I change it I got the error

Code:
void refreshImg() {
 img.loadPixels();
 for (int y = 0; y < height; y++) {
   for (int x = 0; x < width; x++) {
     int px = get();
     img.pixels[x + y * width] = px;
   }
 }
 img.updatePixels();
}


cannot convert from PImage to int

What am I missing?

Cheers
rS
Re: How to get a PImage of the sketch?
Reply #5 - Jul 15th, 2009, 4:48am
 
Quote:
What am I missing?
Two parts of the reference page of get():
"PImage cp = get();"
and
"Reads the color of any pixel or grabs a section of an image. If no parameters are specified, the entire image is returned."
Roll Eyes
Re: How to get a PImage of the sketch?
Reply #6 - Jul 15th, 2009, 4:56am
 
The get() usage is not clear, I dont understant, can you show me in the funciton I wrote?

Cheers
rS
Re: How to get a PImage of the sketch?
Reply #7 - Jul 15th, 2009, 8:17am
 
Code:
void refreshImg() {
img = get(); // Get whole sketch image into img
}
Re: How to get a PImage of the sketch?
Reply #8 - Jul 15th, 2009, 8:36am
 
PhiLho thanks for the support, but it doesnt work, all I get is black,
the only way the code works so far is as I have it using the for loops and get(x, y);

I will research about that, as any optimization is always good

Cheers
rS
Re: How to get a PImage of the sketch?
Reply #9 - Jul 15th, 2009, 9:02am
 
Let see. I take one of my sketches, drawing a little colorful animation.
I add:
Code:

void mousePressed()
{
noLoop();
PImage img = get();
img.filter(THRESHOLD, 0.9);
image(img, width / 2, 0);
}

When I click the mouse, the right half of the sketch displays the filtered (half of) sketch image. So it works for me.
Page Index Toggle Pages: 1