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 › Fill array with images
Pages: 1 2 
Fill array with images (Read 3385 times)
Fill array with images
Jan 17th, 2010, 7:25am
 
to practice with processing I want to do that thing to make one image out of a sequence by taking a vertical strip from 1.jpg an adjacent strip from 2.jpg an adjacent strip from 3.jpg.. Etc
so if I have a bunch of images in a folder how do I fill an array pic[1]..pic[2] with those images?
Cheers

Mick
Re: Fill array with images
Reply #1 - Jan 17th, 2010, 8:37am
 
Here is some code I once used to load images from a folder. It only loads .jpg and .JPG files. (Leave out or change the check if you want to load other filetypes).

Files were located in a submap of the sketch folder called "pictures".

Code:

   //Read the pictures
   {    
     students = new Vector();
     File f1 = new File(sketchPath + "/pictures") ;

     File[] files = f1.listFiles();

     for (int i = 0 ; i < files.length ; i++ ) {
       if(files[i].toString().indexOf(".jpg") != -1 || files[i].toString().indexOf(".JPG") != -1)
       {
         students.add(loadImage(files[i].getAbsolutePath()));
       }
     }
   }
Re: Fill array with images
Reply #2 - Jan 17th, 2010, 8:57am
 
Many thanks. I guessed it would be something with strings but my brain cell is not working so well these days. Undecided
Cheers
Mick
Re: Fill array with images
Reply #3 - Jan 17th, 2010, 9:55am
 
I'm sorry, should that work as is?
I get "syntax error, maybe missing right parenthesis" on line
if(files[i] to String().indexOf.....
And I can't see why
my router has packed up so I can't paste ( using phone)
Mick
Re: Fill array with images
Reply #4 - Jan 17th, 2010, 10:11am
 
Ok  I found typo but still says syntax error
Re: Fill array with images
Reply #5 - Jan 18th, 2010, 6:20am
 
I don't understand Sad
if I copy paste your code it runs but if I try to "tidy" it with some carriage returns it complains "cannot find anything named " students""
I thought spaces made no difference ?
Re: Fill array with images
Reply #6 - Jan 18th, 2010, 6:28am
 
Doh if I paste it it is one line which is entirely commented out
Re: Fill array with images
Reply #7 - Jan 18th, 2010, 6:42am
 
You are not supposed to use the code as is, but to adapt it (I doubt you have students in your code...).
Here is something more adapted to your requirements:
Code:
File picturesPath = sketchFile("pictures"); // Supposes you have a "pictures" folder in your sketch folder
File[] files = picturesPath.listFiles(); // Get a list of files
PImage[] images = new PImage[files.length];
int imageCount = 0;
for (int i = 0; i < files.length; i++) {
   String fileName = files[i].getName().toLowerCase();
   if (fileName.endsWith(".jpg") || fileName.endsWith(".png")) {
 images[imageCount++] = loadImage(files[i].getAbsolutePath());
   }
}
(untested!)
At the end of the loop, imageCount holds the number of loaded images, can be smaller than files.length.
Re: Fill array with images
Reply #8 - Jan 20th, 2010, 4:22am
 
Hi,
I get "cannot convert from PImage[] to PImage"
I've not done much coding and I just started with processing.
I thought I would start by filling an array with the names of the pictures in the directory.
If it just worked I suppose I wouldn't be Reading so much.
Mick
Re: Fill array with images
Reply #9 - Jan 20th, 2010, 4:33am
 
Sorry, I made a typo, should have been PImage[] images = new PImage[files.length]; (also fixed above).
The files variable above is just what you want: an array with a list of files. If you prefer to have a list of file names, use String[] fileNames = picturesPath.list();
The additional loop is used to filter out non-image files, you can drop it if you are sure you will put only images in this directory.
Re: Fill array with images
Reply #10 - Jan 20th, 2010, 10:44am
 
Thanks for your help. I'm getting the hang of it.
How do I get pixels 100 to 199 or 200 to 299 from a 100x100 image using pixels[]
regards
Mick
Re: Fill array with images
Reply #11 - Jan 20th, 2010, 2:14pm
 
michael crane wrote on Jan 20th, 2010, 10:44am:
Thanks for your help. I'm getting the hang of it.
How do I get pixels 100 to 199 or 200 to 299 from a 100x100 image using pixels[]
regards
Mick

I would start with the example shown in the Reference:
Code:
int halfImage = width*height/2;
PImage myImage = loadImage("topanga.jpg");
image(myImage, 0, 0);

loadPixels();
for (int i = 0; i < halfImage; i++) {
 pixels[i+halfImage] = pixels[i];
}
updatePixels();

and tweak that to your advantage.
I guess you would need to have a loop for every image that you load, that runs through the width (or height), something like:
Code:
j++; // j would be equal to the current image number in the series of images, and thus also representing a line in your new image
for (int i = 0; i < myImage.width; i++) {
 newImage.pixels[j*myImage.width+i] = loadedImage.pixels[j*myImage.width+i];
}

Re: Fill array with images
Reply #12 - Jan 21st, 2010, 12:30am
 
Thanks. I realize now that the pixel[] array addresses one pixel at a time. I thought it might be able to addresses a block. It's clearer.
Regards
Mick
Re: Fill array with images
Reply #13 - Jan 21st, 2010, 12:53am
 
You can use copy() to handle blocks of pixels in a friendly/intuitive way.
Re: Fill array with images
Reply #14 - Jan 21st, 2010, 2:46am
 
would copy() be quicker
this is what I have so far from the suggestions.
Code:
 size(533, 400);
File path= sketchFile("pictures");
File[] files= path.listFiles();
PImage[] images = new PImage[files.length];
PImage NewImage=createImage(533,400,RGB);
images[0] = loadImage(files[0].getAbsolutePath());
int count = images[0].width*images[0].height/(files.length);//pixels per image strip
//println(count);
for (int i=0;i<files.length;i++)
{
images[i] = loadImage(files[i].getAbsolutePath());
print(i+ " of "+( files.length)+"; ");
for (int j=0;j<count;j++)
{
loadPixels();
NewImage.pixels[i*count+j]=images[i].pixels[i*count+j];
updatePixels();
}
}
image(NewImage,0,0);


Does that look right ?
it seems slow to make a new image with 10 images and I was hoping to use a thousand. is there a way
to speeds things up ?
cheers

Mick
Pages: 1 2