File Listing Order


#21

exactly, but i could not see that from your above code

thanks.

by the way, the

"extensions=,png

is tricky, from where you know?


say something like

// “extension=js” or “extensions=js|csv|txt” (no dot)


#22

I’ve actually studied its source code in order to know I had to use commas.
And also that an initial comma is needed even for the 1st extension!


#23

When I need to code a very simple sketch, w/o any animations or even functions, I use what is so called “static sketch”.

The standard 1 is called “interactive sketch”, in which we can have the callbacks setup() & draw().

More info on them here: Processing.org/tutorials/overview/

Here’s the my “static sketch” converted to “dynamic sketch”: :grinning:

static final String PICS_EXTS = "extensions=,png,jpg,jpeg,gif,tif,tiff,tga,bmp,wbmp";

void setup() {
  final File dir = dataFile("");
  println(dir);

  String[] imagePaths = {};
  int imagesFound = 0;

  if (dir.isDirectory()) {
    imagePaths = listPaths(dir.getPath(), "files", "recursive", PICS_EXTS);
    imagesFound = imagePaths.length;
  }

  printArray(imagePaths);
  println("Found", imagesFound, "image(s)");

  exit();
}

#24

I’m having trouble using this code to load my images, it seem like “loadimage” wants a string and not a file.

Is there a way to extract the file name from the File? or convert it to a string?

Thanks

Phil



import java.util.Date;


int imageHeight = 800;
PImage img;  
int numFrames = 5;
int currentFrame = 0;
boolean gotFolder = false;
int position=0;

 
 
void setup() {

  noStroke();
  
size(500, 400);
 
  String path = "/Users/philspitler/Downloads/Takeout/GooglePhotos/2018/";

  ArrayList<File> allFiles = listFilesRecursive(path);
 


  for (File f : allFiles) {
      currentFrame++;
   
    println("Filename: " + f);
    
   img = loadImage(f);     //THIS LINE ERRORS
   
        
  }

  noLoop();
  
  
  
}
// Nothing is drawn in this program and the draw() doesn't loop because
// of the noLoop() in setup()
void draw() {

}

// This function returns all the files in a directory as an array of Strings  
String[] listFileNames(String dir) {
  File file = new File(dir);
  if (file.isDirectory()) {
    String names[] = file.list();
    return names;
  } else {
    // If it's not a directory
    return null;
  }
}

// This function returns all the files in a directory as an array of File objects
// This is useful if you want more info about the file
File[] listFiles(String dir) {
  File file = new File(dir);
  if (file.isDirectory()) {
    File[] files = file.listFiles();
    return files;
  } else {
    // If it's not a directory
    return null;
  }
}

// Function to get a list of all files in a directory and all subdirectories
ArrayList<File> listFilesRecursive(String dir) {
   ArrayList<File> fileList = new ArrayList<File>(); 
   recurseDir(fileList,dir);
   return fileList;
}

void recurseDir(ArrayList<File> a, String dir) {
  File file = new File(dir);
  if (file.isDirectory()) {
    // If you want to include directories in the list
    a.add(file);  
    File[] subfiles = file.listFiles();
    java.util.Arrays.sort(subfiles); //Sorts all files by name
    for (int i = 0; i < subfiles.length; i++) {
      // Call this function on all files in this directory
      recurseDir(a, subfiles[i].getAbsolutePath());
    }
  } else {
    a.add(file);
  }
}

#25

That’s because it wants a path to the file, and not the actual file itself.
Replace:

img = loadImage(f);     //THIS LINE ERRORS

With:

img = loadImage(f.getAbsolutePath());     //THIS LINE DOES NOT ERROR :v

#26

Ahhh I thought that just gave me the path, not the filename too…

Thanks.

Phil


#27

arghh one more very odd issue.

It seems like nothing draws to my sketch if I have “img = loadimage(imageName);” in my draw function. If I comment that line out I get a red background as expected. As soon as I add that line nothing draws in my sketch window but the sketch runs an loops through all the file names with errors…



import java.util.Date;


int imageHeight = 400;
PImage img;  
int currentFrame = 0;
int position=0;
String path = "/Users/philspitler/Downloads/Takeout/GooglePhotos/2018/";



void setup() {

  noStroke();


  size(500, 400);
  //frame.setResizable(true);
  background(255, 0, 0);
}



void draw() {   



  ArrayList<File> allFiles = listFilesRecursive(path);

  for (File f : allFiles) {
    currentFrame++;


    String imageName = f.getAbsolutePath();
    if (imageName.endsWith("JPG") ||   imageName.endsWith("jpg")) {
      println("Filename: " + imageName);
      
     img = loadImage(imageName); 
      
  
    }
  }
}








// This function returns all the files in a directory as an array of Strings  
String[] listFileNames(String dir) {
  File file = new File(dir);
  if (file.isDirectory()) {
    String names[] = file.list();
    return names;
  } else {
    // If it's not a directory
    return null;
  }
}

// This function returns all the files in a directory as an array of File objects
// This is useful if you want more info about the file
File[] listFiles(String dir) {
  File file = new File(dir);
  if (file.isDirectory()) {
    File[] files = file.listFiles();
    return files;
  } else {
    // If it's not a directory
    return null;
  }
}

// Function to get a list of all files in a directory and all subdirectories
ArrayList<File> listFilesRecursive(String dir) {
  ArrayList<File> fileList = new ArrayList<File>(); 
  recurseDir(fileList, dir);
  return fileList;
}

void recurseDir(ArrayList<File> a, String dir) {
  File file = new File(dir);
  if (file.isDirectory()) {
    // If you want to include directories in the list
    a.add(file);  
    File[] subfiles = file.listFiles();
    java.util.Arrays.sort(subfiles); //Sorts all files by name
    for (int i = 0; i < subfiles.length; i++) {
      // Call this function on all files in this directory
      recurseDir(a, subfiles[i].getAbsolutePath());
    }
  } else {
    a.add(file);
  }
}


#28

I figured out the issue although not had chance to figure out a solve…

Basically the draw function doesn’t update until the entire for loop is done and therefore nothing is drawn.

Phil


#29

Multiple things all at once:

  • That fix for your first error I gave you is because it was literally giving the File itself - the .getAbsolutePath() now gives a String with full path with the filename, and not that File as an object.

  • And, in your current issue, the problem is that img = loadImage(imageName); loads the image from the disk and puts it into variable img, but nothing else. You have to tell it to draw it to the screen.

  • Also, every time you do img = loadImage(imageName); you are removing what was there in img before by putting a new image into it.

  • That code you have in draw() goes through all folders and directories inside of your folder that you set, and sets img to each one of them, and by the moment the code ends img has only the last image.

  • Also, it’s best to use loadImage(... function only in setup(), as draw() is run 60 times a second, and loading your image(s) from your hard drive 60 times a second is, well, pretty taxing for your hard drive and the sketch’s performance - it’s better to store it in that variable img, and then, inside of draw(), do image(img, 0, 0); to actually draw the image img from top left corner(which coordinates 0 0 point to).

Overall, I suggest you to look and replicate this example sketch:
https://processing.org/examples/loaddisplayimage.html


#30

Thanks @Architector_4 I had stripped out the code that was actually Processing each image to help debug.

Basically my code does work but doesn’t update my draw window until it has looped through all my thousands of images and Processed them.

That works for this purpose as I process each image then save a colored barcode which is visual representation of my photos of 2018.

Thanks for all your help. I will try and figure out how to avoid the For loop when I get chance but at least I got this done and posted within the first week of 2019 :slight_smile:


#31

Oh, that’s actually a pretty clever idea!
Although it could be better done by loading and displaying only 1 image at a time in draw() - a variable frameCount helps with that, as it tells which time the draw() function is currently run.
Here, this sketch gets all the images and makes a barcode thing while visually flashing every image it puts on the screen. You’ll have to set horizontal size manually though - set it to the amount of images to don’t leave empty space or miss out on images on the right.

String PICS_EXTS = "extensions=,png,jpg,jpeg,gif,tif,tiff,tga,bmp,wbmp";
  //Picture extensions
String[] images;
  //Where we are going to put our images
int ySize = 500;
  //Vertical size
float barCodeStretch = 1;
  //Increase to something like 500 to stretch the image to form the barcode like thing.
  
void settings(){
  size(1000,ySize); //Set window size
}

void setup() {
  String path = dataPath("");
  //Path of where to look for all the iamges
  
  images = listPaths(path, "recursive", "files", PICS_EXTS);
  println("We found "+images.length+" images.");
}

void draw(){
  if(frameCount<images.length){
    image(loadImage(images[frameCount]),frameCount,-(ySize/2*(barCodeStretch-1))/2,ySize,ySize*barCodeStretch);
    //I don't know the validity of the maths for the position/scale above, but it certainly does stretch things.
  }else{
    println("Saving image...");
    saveFrame(dataPath("barcode.png"));
    println("Saved. Closing in 10 seconds");
    delay(10000); //Wait 10 000 milliseconds.
    exit();
  }
}

#32

This is very similar to how I had it before implementing the listpaths function to sort the files alphabetical.

Thanks for taking the time to help me with this…

Phil


#33

I get a “function does not exist” error here.

images = listPaths(path, “recursive”, “files”, PICS_EXTS);

Maybe you changed how the directory listing worked?

Could you post the full code.

Cheers

Phil


#34

i play @Architector_4 example
and when i save the project and create a /data/ dir with some pictures in it
the function works here. ( means also: it is the full code )

note: storing the resulting picture at the same path will on second run list this too.

so as the function not run from your code, you might post
your code here / possibly we find the problem /


#35
String PICS_EXTS = "extensions=,png,jpg,jpeg,gif,tif,tiff,tga,bmp,wbmp";
  //Picture extensions
String[] images;
  //Where we are going to put our images
int ySize = 500;
  //Vertical size
float barCodeStretch = 1;
  //Increase to something like 500 to stretch the image to form the barcode like thing.
  
void settings(){
  size(1000,ySize); //Set window size
}

void setup() {
  String path = dataPath("");
  //Path of where to look for all the iamges
  
  images = listPaths(path, "recursive", "files", PICS_EXTS);
  println("We found "+images.length+" images.");
}

void draw(){
  if(frameCount<images.length){
    image(loadImage(images[frameCount]),frameCount,-(ySize/2*(barCodeStretch-1))/2,ySize,ySize*barCodeStretch);
    //I don't know the validity of the maths for the position/scale above, but it certainly does stretch things.
  }else{
    println("Saving image...");
    saveFrame(dataPath("barcode.png"));
    println("Saved. Closing in 10 seconds");
    delay(10000); //Wait 10 000 milliseconds.
    exit();
  }
}



#36

that works fine here,
under above mentioned project environment
if you just run it in a new processing 3.4 window i see:

insofar @GoToLoop code was more failsafe.

pls show us the error ?picture?


#37

Ahh… I figured it out… I had Processing 2.21 open.

I tried it in Processing 3 and it worked…

However, this is now going back to an earlier problem of the images not being sorted alphabetically.

Example output below.

Phil

We found 2537 images.
/Users/philspitler/Downloads/Takeout/GooglePhotos/2018/IMG_20180802_214451.jpg
/Users/philspitler/Downloads/Takeout/GooglePhotos/2018/IMG_20180930_144223.jpg
/Users/philspitler/Downloads/Takeout/GooglePhotos/2018/IMG_20180622_103332.jpg
/Users/philspitler/Downloads/Takeout/GooglePhotos/2018/IMG_20180531_082254.jpg
/Users/philspitler/Downloads/Takeout/GooglePhotos/2018/IMG_20180105_164421_700.jpg
/Users/philspitler/Downloads/Takeout/GooglePhotos/2018/IMG_20180320_130226.jpg


#38

did you try a

  images = sort(images);

looks good here.

or lets go back:
did that work while you use this


code? ( @architector_4 version with sort and selected file types )
if ok, you can all posts below that consider a academic exercise.


#39

Worked perfectly… Thanks again.

Phil


#40

Oh, yes, right, I forgot what the actual topic was about and forgot to include sort(images); in my actual code. Sorry! :crazy_face: