load folders of Pimages - multiple folders, unknown numbers, non-sequential filenames

hi all,

Happy new year.

Here's a problem i'm labouring over these last few days.

I want to load folders of images into an array of Pimages.

requirements: separate folders with the datapath. number of images will vary and will not be sequentially named (if possible).

so i guessed i'd need an arraylist and append the images into it.

This is how far i got. Clearly i'm muddling my arrays, strings etc.

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() {
  boolean accept(File dir, String name) {
    return name.toLowerCase().endsWith(".jpg");
  }
};

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

// get the number of jpg files
println(filenames.length + " jpg files in specified directory");

PImage [] imgAsset = new PImage [filenames.length];

for (int i = 0; i < filenames.length; i++) { 
  imgAsset[i] = loadImage(filenames[i]);
}

image(imgAsset[5], 0, 0); 

//the 5 in the above is to test the array. it will be a variable in the final code.

Answers

  • yeah i did see that. Couldn't figure out the Pimage call.

  • thanks again @gotoloop. I enjoyed your verbose responses!

    TIL - how to use strings as url.

    final code:

    size(1000, 1000);
    ArrayList<PImage> imgAsset;
    
    
    import java.io.FilenameFilter;
    
    final FilenameFilter PIC_FILTER = new FilenameFilter() {
      final String[] exts = {
        ".png", ".jpg", ".jpeg", ".gif"
      };
    
      @ Override boolean accept(final File dir, String name) {
        name = name.toLowerCase();
        for (final String ext : exts)  if (name.endsWith(ext))  return true;
        return false;
      }
    };
    
    File folder = dataFile("");
    println(folder);
    
    File[] pics = folder.listFiles(PIC_FILTER);
    println("# of files found at folder above:", pics.length, ENTER);
    printArray(pics);
    println();
    
    imgAsset = new ArrayList<PImage>();
    
    String[] filenames = new String[pics.length];
    for (int i = 0; i < pics.length; filenames[i] = pics[i++].getPath());
    
    for (int i = 0; i < pics.length; i++) {
      //imgAsset.add(filenames[i], "jpg");
    
      //imgAsset[i] = loadImage(filenames[i], "jpg");
      PImage p = loadImage(filenames[i], "jpg");
      imgAsset.add(p);
    };
    
    image(imgAsset.get(round(random(pics.length))), 0, 0);
    
  • edited January 2017

    how to use strings as url.

    Well, URL are strings w/ things like http:// on it. :-\"

    Although I don't get what URLs got anything to do w/ FilenameFilter and dataFile(). 8-|

  • ok, so how do i have separate folders of images that all add into the main arraylist? i figure i loop through the code appending each time, but it's getting very muddled.

  • edited January 2017
    /**
     * FilenameFilter + isDirectory() (v1.0)
     * GoToLoop (2017-Jan-05)
     *
     * forum.Processing.org/two/discussion/20063/
     * load-folders-of-pimages-multiple-folders-
     * unknown-numbers-non-sequential-filenames#Item_7
     *
     * forum.Processing.org/two/discussion/17111/
     * how-to-display-a-random-image-using-processing#Item_3
     */
    
    import java.util.List;
    import java.io.FileFilter;
    import java.io.FilenameFilter;
    
    final FileFilter FOLDER_FILTER = new FileFilter() {
      @ Override boolean accept(final File path) {
        return path.isDirectory();
      }
    };
    
    final FilenameFilter PIC_FILTER = new FilenameFilter() {
      final String[] exts = {
        ".png", ".jpg", ".jpeg", ".gif"
      };
    
      @ Override boolean accept(final File path, String name) {
        name = name.toLowerCase();
        for (final String ext : exts)  if (name.endsWith(ext))  return true;
        return false;
      }
    };
    
    File dataFolder = dataFile("");
    File[] imgDirs = dataFolder.listFiles(FOLDER_FILTER);
    imgDirs = (File[]) append(imgDirs, dataFolder);
    
    println(imgDirs.length, "folders found:");
    printArray(imgDirs);
    
    List<File[]> imgPaths = new ArrayList<File[]>();
    for (File dir : imgDirs)  imgPaths.add(dir.listFiles(PIC_FILTER));
    
    println("\nImage paths found for all folders:");
    
    int totalLength = 0;
    for (File[] paths : imgPaths) {
      totalLength += paths.length;
      println();
      printArray(paths);
    }
    
    println("Total images found in all subfolders:", totalLength);
    
    PImage[] imgs = new PImage[totalLength];
    int idx = 0;
    for (File[] paths : imgPaths)  for (File f : paths)
      imgs[idx++] = loadImage(f.getPath());
    
    exit();
    
  • /**
     * FilenameFilter + isDirectory() (v1.0)
     * GoToLoop (2017-Jan-05)
     *
     * forum.Processing.org/two/discussion/20063/
     * load-folders-of-pimages-multiple-folders-
     * unknown-numbers-non-sequential-filenames#Item_7
     *
     * forum.Processing.org/two/discussion/17111/
     * how-to-display-a-random-image-using-processing#Item_3
     */
    
    import java.util.List;
    import java.io.FileFilter;
    import java.io.FilenameFilter;
    
    final FileFilter FOLDER_FILTER = new FileFilter() {
      @ Override boolean accept(final File path) {
        return path.isDirectory();
      }
    };
    
    final FilenameFilter PIC_FILTER = new FilenameFilter() {
      final String[] exts = {
        ".png", ".jpg", ".jpeg", ".gif"
      };
    
      @ Override boolean accept(final File path, String name) {
        name = name.toLowerCase();
        for (final String ext : exts)  if (name.endsWith(ext))  return true;
        return false;
      }
    };
    
    PImage[] imgs; 
    
    void setup() { 
      size (777, 777);
    
      File dataFolder = dataFile("");
    
      File[] imgDirs = dataFolder.listFiles(FOLDER_FILTER);
      imgDirs = (File[]) append(imgDirs, dataFolder);
    
      println(imgDirs.length, "folders found:");
      printArray(imgDirs);
    
      List<File[]> imgPaths = new ArrayList<File[]>();
    
      for (File dir : imgDirs) 
        imgPaths.add(dir.listFiles(PIC_FILTER));
    
      println("\nImage paths found for all folders:");
    
      int totalLength = 0;
      for (File[] paths : imgPaths) {
        totalLength += paths.length;
        println();
        printArray(paths);
      }
    
      println("Total images found in all subfolders:", totalLength);
    
      imgs = new PImage[totalLength];
      int idx = 0;
      for (File[] paths : imgPaths)
        for (File f : paths)
          imgs[idx++] = loadImage(f.getPath());
    
      // exit();
    }
    
    void draw() {
    
      int y1 = 70;
      for (PImage img1 : imgs) {
        image(img1, 30, y1);
        y1 += img1.height+33 ;
      }
    }
    //
    
Sign In or Register to comment.