Loading...
Logo
Processing Forum
Hi all,
I'm would like to use the build-in java class  java.io.File to collect the number of .jpg in my data folder.
When I want to use it in my code, to set a int, I got a message error "Cannot convert int[] to int"
I want to convert it to a int because my code'd display randomly one of the jpg from my data folder.
The error comes from line 21.
Copy code
  1. void setup(){

  2.     // have a look in the data folder
  3.     java.io.File folder = new java.io.File(dataPath(""));

  4.     // this is the filter (returns true if file's extension is .jpg)
  5.     java.io.FilenameFilter jpgFilter = new java.io.FilenameFilter() {
  6.       public boolean accept(File dir, String name) {
  7.         return name.toLowerCase().endsWith(".jpg");
  8.       }
  9.     };

  10.     // list the files in the data folder
  11.     String[] numberofpics = folder.list(jpgFilter);

  12.     // get the number of jpg files
  13.     println(numberofpics.length + " jpg files in specified directory");
  14.     
  15.     int numberofpics_int = int (numberofpics);
  16.     xPic = numberofpics_int;
  17.     
  18.     //Pic loading
  19.     Pic = random(1,xPic);
  20.     int PicFragNumber = int (Pic);
  21.     img = loadImage("Pic_" + PicFragNumber +".jpg");
Does anyone know how to do this magic trick?

Thank's a lot.



Replies(5)

Ok, to be honest, this looks like a little bit of copy&paste.
"folder.list(jpgFilter)" returns an array of filenames. The length of this array is the number of jpg-images in your folder.
No need for converting this array to an int-array or something.

And you should maybe just keep that filename-array for later use when loading the images, so you don't have to name your files "1.jpg", "2.jpg", etc.

Something like that:
Copy code
  1. String[] fileNames;

    void setup() {
      // have a look in the data folder
      java.io.File folder = new java.io.File(dataPath(""));

      // this is the filter (returns true if file's extension is .jpg)
      java.io.FilenameFilter jpgFilter = new java.io.FilenameFilter() {
        public boolean accept(File dir, String name) {
          return name.toLowerCase().endsWith(".jpg");
        }
      };

      // get a list of jpg-files in the data folder
      fileNames = folder.list(jpgFilter);
      println(fileNames.length + " jpg files in specified directory");

      // load random jpg-image, if there are any
      if (fileNames.length > 0) {
        int picNumber = ceil(random(0, fileNames.length));
        PImage img = loadImage(fileNames[picNumber]);
        image(img, 0, 0);
      }
    }

Benja!
I start to believe you're my code guardian angel ;-)
Of course it's copy paste!
You know my skill in coding now :)
Here is the original discussion where I got it from:

It works great, thank's one thousands times!
I even don't have to rename the files with Adobe Bridge now.
Plus I add ".jpeg" in the jpg filter so I don't have to change the extension neither anymore.

I'm just running out one last problem.

Once in a while the code loads the last pic or gif, causing a 
"ArrayIndexOutOfBoundsException: 1223 (It's the number of my pics in the data folder)

I really don't get it...
It checks the number of pics (.jpg || .jpeg)
If I println it, it's exactly the right number of files.

I mean I have a .jpg called "Pic_1223.jpg" so why is it "out of bounds"?
It does exactly the same with the last .gif files.

Why the array get's smaller than the number of files??

It will be easier with the code :)

Copy code
  1. void setup() {
  2.     
  3.     size(1300,760);
  4.     imageMode(CENTER);
  5.     
  6.     // have a look in the data folder. 
  7.     java.io.File folder = new java.io.File(dataPath(""));
  8.     
  9.     // Set the filter for .jpg. List the jpg and number the files
  10.     java.io.FilenameFilter jpgFilter = new java.io.FilenameFilter() {
  11.       public boolean accept(File dir, String name) {
  12.         return name.toLowerCase().endsWith(".jpg") || name.toLowerCase().endsWith(".jpeg");
  13.       }
  14.     };
  15.     numberofPic = folder.list(jpgFilter);
  16.   
  17.     
  18.     // Set the filter for .gif. List the gif and number the files
  19.     java.io.FilenameFilter gifFilter = new java.io.FilenameFilter() {
  20.       public boolean accept(File dir, String name) {
  21.         return name.toLowerCase().endsWith(".gif");
  22.       }
  23.     };
  24.     numberofGif = folder.list(gifFilter);
  25.   }
  26.   
  27.  

  28.   void draw() {
  29.     
  30.     background (255);
  31.     
  32.     // Wait for the Pic or animation to be displayed to launch the program
  33.     if (displayTime <= millis() - displayStart) {
  34.   
  35.       // Random for choosing between a Gif and a Pic
  36.       if (random(numberofPic.length + numberofGif.length) > (numberofGif.length)) {
  37.     
  38.         // Pick new jpg-image
  39.         showGif = false;

  40.         pictoDisplay = ceil(random(1, numberofPic.length));
  41.         img = loadImage(numberofPic[pictoDisplay]);
  42.         println( "PIC - " + pictoDisplay);
  43.         
  44.         // Set the display length of the Pics
  45.         displayTime = 400;
  46.         displayStart = millis();
  47.       }
  48.       
  49.       else {
  50.         
  51.         // Pick new gif-image
  52.         showGif = true;

  53.         giftoDisplay = ceil(random(1, numberofGif.length));
  54.         animation = Gif.getPImages (this, numberofGif[giftoDisplay]);
  55.      
  56.         //number of repetitions
  57.         float r = random (2,4);
  58.         int gifrepeats = int (r);
  59.           
  60.         // load gif-animation and delay-times
  61.         delays = getDelays(numberofGif[giftoDisplay]);
  62.           
  63.         println ("GIF - " + giftoDisplay + " repeats:"+gifrepeats);
  64.           
  65.         // calculate length of the whole animation 
  66.         int gifTime = 0;
  67.         for (int i = 0; i< animation.length; i++) {
  68.           gifTime += delays[i];
  69.         }
  70.           
  71.         displayTime = gifrepeats*gifTime;
  72.         displayStart = millis();
  73.         currentFrame = 0;
  74.       }
  75.     }
  76.       
  77.       
  78.     // Showing the Pic or Animation

  79.     if(showGif){
  80.       
  81.       if (millis()-frameStart > delays[currentFrame]) {
  82.         currentFrame = (++currentFrame)%animation.length;
  83.         frameStart = millis();
  84.       }
  85.        
  86.       image(animation[currentFrame], width/2, height/2);
  87.     }
  88.     
  89.     else{
  90.   
  91.       smooth();
  92.       if (img.width>screen.width) { img.resize((screen.width - 10),0); } // conditional resize width
  93.       if (img.height>screen.height) { img.resize(0,(screen.height - 10)); } // conditional resize height
  94.       image(img, width/2, height/2); // display image in center of the sketch
  95.     }
  96. }
Arrays start with index 0, so the last index of an array with 1223 items is 1222.
it was my fault, you shouldn't use ceil(), try floor() instead.
Copy code
  1.         giftoDisplay = floor(random(0, numberofGif.length));
Perfect!
Thank's a lot for all your time Benja.