How to display a random image using Processing

edited April 2017 in How To...

Hello everyone, first of all I will present myself, I'm a 20 years old french student in graphic design, sorry if I make mistake in english. It's my first time using processing, I don't know nothing in coding, but I would enjoy to learn in order to create awesome functions like I see in this forum! I'm on a graphic design project, it's for a brand identity, I'm creating a modulable logo using random image selection so I would need a code to display a random image from an image folder. I've been searching in this forum and on varions websites, and I found the instructions to how to code it, but after various fail I'm understanding that it isn't as easy as I thought... Because I'm time limited for my project, I wish one of you could send me the code to copy and paste, to be immediatly functional. I know I'm asking you to do it for me, but I really don't have the time to learn coding 1 week before the presentation. It isn't a remunerated project, it's for presenting my branding concept to my teachers. Thank you for taking time to read and answer, love from Strasbourg !

Answers

  • edited June 2016

    Thank you for your help, I tried to copy your code to try it, but it fail, I might have forget something, the sketchpad open but keep loading, I have to close it with alt + shift + esc. In order to display randomly an image from a file, I have to create the file folder with the img in it, naming it and replace "images" by this name folder in your code is it ?

  • edited April 2017

    I might have forget something, ...

    • It seems you've forgotten to read that forum thread before running the code! [-(
    • The way the sketch is set, it assumes you've gotta a subfolder "data/images/" w/ all of your images inside.
    • However I've also forgotten the fact there can be spurious files there as well, like ".DS_STORE" and ".db".
    • I turn them off in my OS. But for most folks we have to assume all folders got additional hidden system files. 8-|
    • But fret not, here comes FilenameFilter for the rescue: \m/ http://docs.Oracle.com/javase/8/docs/api/java/io/FilenameFilter.html

    /**
     * FilenameFilter + dataFile() (v1.0.2)
     * GoToLoop (2016-Jun-13)
     *
     * forum.Processing.org/two/discussion/17111/
     * how-to-display-a-random-image-using-processing#Item_3
     */
    
    import java.io.FilenameFilter;
    
    final FilenameFilter PIC_FILTER = new FilenameFilter() {
      final String[] EXTS = {
        ".png", ".jpg", ".jpeg", ".gif", ".tif", ".tiff", ".tga"
      };
    
      @ 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("images");
    println(folder);
    
    File[] pics = folder.listFiles(PIC_FILTER);
    println("# of files found at folder above:", pics.length, ENTER);
    printArray(pics);
    println();
    
    String[] filenames = new String[pics.length];
    for (int i = 0; i < pics.length; filenames[i] = pics[i++].getPath()); 
    
    println(filenames);
    exit();
    
Sign In or Register to comment.