Loading...
Logo
Processing Forum
Hi, sorry to be a massive pain

Loving Processing, but I am a total code noob.  I don't really have any experience of programming at all.
I have a folder with 551 images in it, I want to load one of them at random.  I know about PImage and random() and void()
Do I need to make a huge array with all of the file names in it?  Or is there a way in the Terminal to pull off the file list and paste into Processing?  All the files have a name which is between 3 and 5 random digits.  Don't mind renaming them all in a batch if that is easier.  I'm on a mac, if that wasn't obvious.
This is the first thing that I want to learn how to do.  Then I want to creatively 'break' the image.  But that is something I will learn on my own, with a little help from the Processing tutorials.

Thanks in advance

Replies(10)

If you can rename the images including a sequential number (easy enough to do, for example with IrfanView) then it would be easy to generate a filename at random and then pass this to loadImage...

Note that you might find nfs() useful if you land up with extra zero padding on the numbers in your filenames (e.g. 'img001.jpg').

Why would it be obvious that you are on a Mac?
OK, so I did this:
Copy code
  1. void setup() {
  2.   size(176*2, 132*2);
  3.  PImage fragment;
  4. float a = random(100,552);
  5. int fragNumber = int (a);
  6.   fragment = loadImage("frag" + fragNumber +".jpg");
  7.   image(fragment, 0, 0, fragment.width*2, fragment.width*2);
  8.   smooth();
  9. noLoop();
  10. }
Might be a little clunky, but it's a WIP.
I renamed the files with Bridge CS4 (obvious really).  Irfanview is not available for mac.  Didn't need to use nfs() because I started the filenames at 100 and went up to 651.  

If woking with one of the images from the lib is not a single occurence is your program, and if your want just to display one of these images randomly after, say a user pushes a button and it is some sort of a repetitive or multiple times used action, I would recommend first loading all the images into an array and then getting one of them at random by referring to a random index of your images array. This way is more sane since you get all the images pre-loaded which is generally a good thing to do because of numerous performance and convinience bonuses.
I would recommend first loading all the images into an array and then getting one of them at random by referring to a random index of your images array.
Bad idea, it is the best way to run out of memory and lag a lot at start up time... That's 551 images there! Now, one can load an image in advance, while waiting for an action.

Note: to avoid renaming, it wasn't a bad idea to load the file names in an array of strings. You can do that automatically with Java's File's list() function.



I thought about loading them all up into an array, but is there an easy way to do this without writing out endless lines of code?
Is there such a way to have a self populating array?
I have nearly 600 images that I want to use in this project.  Due to the way they are compressed, they total about 17Mb.
I am not entirely sure what I want to do with them so far, they are cut up images of collage that I have made.
Basically I want to 'fuck' with them.  Glitch them up, combine them in unusual ways, play around in an interactive way.
I'm sure that my use of them will continue in an organic and improvisational way, just as the way I developed the images.

wrote my response at the same time as  phi.lho there... so I need to use the list() function.  Will research.  Cheers

See File.list()
Use it as:
File path = new File("/path/to/the/images");
String[] images = path.list();
And then you can iterate on the list of images.
To tone down my remark above: if the images are small (a few KB), you can load a number of them at once. The issue some people had is when you try and load a bunch of images of the size of the screen!
Don't forget that an image can weight 100KB on disk and use 1MB or more in memory: they are uncompressed after loading.
in the shell create a file containing all the names:
ls files > file_list

then use loadStrings("file_list");

(File.list() will also return directories and all other files in the directory. you can filter them out.)
I modified the code slightly.  I decided that I didn't need to load all 550+ images, and that I would load up 5 to use in each instance of the program.  Thus:



Copy code
  1. void setup() {
  2.   size(660, 132);
  3.  PImage fragment1;
  4.  PImage fragment2;
  5.  PImage fragment3;
  6.  PImage fragment4;
  7.  PImage fragment5;
  8.  
  9. float a = random(100,653);
  10. int fragNumber1 = int (a);
  11. fragment1 = loadImage("frag" + fragNumber1 +".jpg");

  12. float b = random(100,653);
  13. int fragNumber2 = int (b);
  14. fragment2 = loadImage("frag" + fragNumber2 +".jpg");

  15. float c = random(100,653);
  16. int fragNumber3 = int (c);
  17. fragment3 = loadImage("frag" + fragNumber3 +".jpg");

  18. float d = random(100,653);
  19. int fragNumber4 = int (d);
  20. fragment4 = loadImage("frag" + fragNumber4 +".jpg");

  21. float e = random(100,653);
  22. int fragNumber5 = int (e);
  23. fragment5 = loadImage("frag" + fragNumber5 +".jpg");

  24.   
  25.   image(fragment1, 0, 0, 660, fragment1.height*2);
  26.   tint(0);
  27.   smooth();
  28. noLoop();
  29. }
Again, sure there is a more elegant way to do this, but I'm just learning.  Cheers for the input so far though.
Hi there codeless. Here's my solution.

Copy code
  1. // DECLARE YOUR GLOBAL VARIABLES HERE.
    PImage fragment;
    int rand;

    void setup() {
     size(800, 600);
     rand = int(random(0,9)); //HERE YOU CHOOSE BETWEEN 10 DIFFERENT IMAGES
     takerandomimage("frag_" + nf(rand, 3) + ".jpg"); //CALL YOUR FUNCTION 'takerandomimage'
     //REMEMBER TO NAME YOUR IMAGES "frag_000.jpg"
    }
    // THIS IS THE FUNCTION
    void takerandomimage(String fn) {
       fragment = loadImage(fn); //LOAD RANDOM IMAGE
       image(fragment,0,0);//DISPLAY RANDOM IMAGE
    }