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 › Question about PImage syntax.
Pages: 1 2 
Question about PImage syntax. (Read 3552 times)
Question about PImage syntax.
Oct 18th, 2009, 3:58pm
 
Hi i want to import a image sequence just like in this example :

http://processing.org/learning/topics/sequential.html



but , i dont understand how to not enter amnually all my framnes ( 250 frames )  

it says :

create the filenames as the program runs.
 // The nf() command does number formatting, which will
 // ensure that the number is (in this case) 4 digits.
 //for(int i=0; i<numFrames; i++) {
 //  String imageName = "PT_anim" + nf(i, 4) + ".gif";
 //  images[i] = loadImage(imageName);


can you explain me what to do to make processing load them automaticaly ?

and where should i copy the image sequence. in a folder inside my sketch folder,  or direcly inside my sketch folder ?

thanx!

Re: Question about PImage syntax.
Reply #1 - Oct 18th, 2009, 7:27pm
 
If Gif is ok for you. you can use the gif image library. That makes it really easy as you dont have to import every frame as single image but can just import the animated gif and get every frame you want
http://processing.org/discourse/yabb2/?num=1199371244
Re: Question about PImage syntax.
Reply #2 - Oct 18th, 2009, 7:52pm
 
// The nf() command does number formatting, which will
// ensure that the number is (in this case) 4 digits.
//for(int i=0; i<numFrames; i++) {
//  String imageName = "PT_anim" + nf(i, 4) + ".gif";
//  images[i] = loadImage(imageName);

^ this example assumes that your images have the following name format:
PT_anim0001, PT_anim0002, PT_anim0003...//...PT_anim0250.

It works by creating a String, or piece of text, titled "imageName" for reference.  Inside that string it puts the text "PT_anim" , followed by the number i, which is running in a loop from 0 to (in your case) 250.  In this example the file need 0s padding the "i" number, so the nf() function is used.  Then the array of PImages is filled, using that String to name the file to load...

the images go in a folder named "data" (lowercase, no quotes) inside the sketch folder.
Re: Question about PImage syntax.
Reply #3 - Oct 19th, 2009, 10:32am
 
thanx !


i created thew data folder with my images :

splat30012.png
splat30013.png
...   ...  ...
splat30250.png


and my code is this:

Code:
int numFrames = 250;  // The number of frames in the animation
int frame = 0;
PImage[] images = new PImage[250];

void setup()
{
size(800, 300);
frameRate(30);



// If you don't want to load each image separately
// and you know how many frames you have, you
// can create the filenames as the program runs.
// The nf() command does number formatting, which will
// ensure that the number is (in this case) 4 digits.

for(int i=0; i<numFrames; i++) {
String imageName = "splat30012" + nf (i, 4) + ".png";
images[i] = loadImage(imageName);
}
}

void draw()
{
frame = (frame+1) % numFrames; // Use % to cycle through frames
image(images[frame], 50, 50);
}



i get the    Nullpointer exeption  error

any ideas ?

thanx!
Re: Question about PImage syntax.
Reply #4 - Oct 19th, 2009, 11:02am
 
You can't add to the string directly, because with quotes around it, "splat30012" is a string and not a number.  You need this:

"splat" + 30012 + i + ".png";

(no need for nf() since you won't have any numbers with fewer than 5 digits)
Re: Question about PImage syntax.
Reply #5 - Oct 19th, 2009, 11:55am
 
thanx!

I changed that , butit still makes the error , nullpointexeption



and the animation is not played i just see a grey background.

this is my new code:

Code:
int numFrames = 250;  // The number of frames in the animation
int frame = 0;
PImage[] images = new PImage[250];

void setup()
{
size(800, 300);
frameRate(30);



// If you don't want to load each image separately
// and you know how many frames you have, you
// can create the filenames as the program runs.
// The nf() command does number formatting, which will
// ensure that the number is (in this case) 4 digits.

for(int i=0; i<numFrames; i++) {
String imageName = "splat" + 30012 + i + ".png";
images[i] = loadImage(imageName);
}
}

void draw()
{
frame = (frame+1) % numFrames; // Use % to cycle through frames
image(images[frame], 50, 50);
}

Re: Question about PImage syntax.
Reply #6 - Oct 19th, 2009, 2:59pm
 
TweakingKnobs wrote on Oct 19th, 2009, 10:32am:
thanx !


i created thew data folder with my images :

splat30012.png
splat30013.png
...   ...  ...
splat30250.png


Do your image filenames start at 30012  In which case you clearly have less than 250 images and that would give you a null pointer exception.  Perhaps you have 30250 - 30012 image files   Huh
Re: Question about PImage syntax.
Reply #7 - Oct 20th, 2009, 1:58am
 
You need parentheses around the arithmetic operation, otherwise the numbers are just concatenated:
String imageName = "splat" + (30012 +  i) + ".png";

Or just use:
for (int i = 30012; i < 30012 + numFrames; i++) {
String imageName = "splat" + i + ".png";
images[i] = loadImage(imageName);
}

(I would make the magic number 30012 a constant.)
Re: Question about PImage syntax.
Reply #8 - Oct 20th, 2009, 2:22pm
 
thanx !

i tried it both ways, the first gives me again a nullpointerexeption , and the second way an

arrayindexaout of bounds exeption : 30012


why is wrong ?

Code:
int numFrames = 238;  // The number of frames in the animation
int frame = 0;
PImage[] images = new PImage[238];

void setup()
{
size(800, 300);
frameRate(30);





for (int i = 30012; i < 30012 + numFrames; i++) {
String imageName = "splat" + i + ".png";
images[i] = loadImage(imageName);
}
}

void draw()
{
frame = (frame+1) % numFrames; // Use % to cycle through frames
image(images[frame], 50, 50);
}


thanx!
Re: Question about PImage syntax.
Reply #9 - Oct 20th, 2009, 3:16pm
 
The problem with the second method is here:

for (int i = 30012; i < 30012 + numFrames; i++) {
String imageName = "splat" + i + ".png";
images[i] = loadImage(imageName);
}

Whilst PhilHo's shortcut makes generating the filename simpler you can't then use i to reference the position in the array:  You're referencing index 30012 in an array of 238 objects, hence the out of bounds exception.  It is however possible to define multiple variables in a for loop:

Code:
for (int i=0, j=30012; i<numFrames; i++, j++) {
 // use j for the filename
 String imageName = "splat" + j + ".png";
 // and use i for the array reference...
 images[i] = loadImage(imageName);
}


Actually - I remember seeing unexpected null pointer exceptions which might account for the first method not working.  Try:

int numFrames = 238;  // The number of frames in the animation
int frame = 0;
PImage[] images;

void setup()
{
 size(800, 300);
 frameRate(30);
 // setup the array in setup
 images = new PImage[238]
 ...


That's just an untested hunch - could be a bug, or there might be a good reason why you shouldn't create an array of PImages outside of setup - or my memory of this issue may be completely wrong...


Actually I think that might be misleading.  Try the first suggested method above and if that doesn't work post the code.
Re: Question about PImage syntax.
Reply #10 - Oct 20th, 2009, 11:44pm
 
Oups! Smiley

I don't see how the first method can do a NullPointerException. Indicating where you have such exception (which line) can help.
Re: Question about PImage syntax.
Reply #11 - Oct 20th, 2009, 11:51pm
 
wow!  I never knew you could have more than one variable in a for() loop declaration.  Thanks, blindfish.
Re: Question about PImage syntax.
Reply #12 - Oct 21st, 2009, 6:18am
 
PhiLho  wrote on Oct 20th, 2009, 11:44pm:
Oups! Smiley

I don't see how the first method can do a NullPointerException. Indicating where you have such exception (which line) can help.


As I indicated in my crossed out comment above - I'm sure I've come across this when I've defined and populated some variable types before setup.  Of course right now I don't have a reproducible example  Roll Eyes

When I get the chance I'll see if I can reproduce the problem...
Re: Question about PImage syntax.
Reply #13 - Oct 21st, 2009, 11:47am
 
OK - I can reproduce the problem I referred to (in Processing 1.0.7 on Windows XP) which, as I mentioned before but crossed out, happens when you try to load a PImage before running setup:

Code:
PImage a = loadImage("connected.jpg");

void setup() {
 size(300, 200);
 image(a,0,0);
}


...I get a NullPointerException on "image(a,0,0);".  If I try:

Code:
PImage a;
a = loadImage("connected.jpg");

void setup() {
 size(300, 200);
 image(a,0,0);
}


I get "unexpected token: void", which seems a little odd.  This however works just fine:

Code:
PImage a;


void setup() {
 a = loadImage("connected.jpg");
 size(300, 200);
 image(a,0,0);
}


Looks like a bug to me...

Having said that I couldn't reproduce the problem using the OP's approach of defining the array first and then loading the images in setup.

TweakingKnobs - if you're still having the problem (and it's not related to the above) you would do well to post some code.
Re: Question about PImage syntax.
Reply #14 - Oct 21st, 2009, 12:21pm
 
In the code shown in this thread, the images are loaded in setup().
What you illustrate isn't a bug, but a normal behavior: all loadXxx calls should be done in setup(), and certainly not at global level.
Why? Because it otherwise they will be executed before setup() is called. But it is at setup() time that the location of the 'data' folder is computed. So loadImage() outside of it will fail (image not found), hence the NPE.
Pages: 1 2