print the filename and path of an image

Is it possible to display the filename and the path of an image that has been loaded in setup? I tried to just print the image variable - I get this: "processing.core.PImage@28b301f2"

PImage img;  // Declare variable "a" of type PImage

void setup() {
  size(640, 360);
  img = loadImage("moonwalk.jpg");  // Load the image into the program  
}

void draw() {
  image(img, 0, 0);
  println(img);// try to print the image filename (and maybe it's path?)
}

Answers

  • edited March 2014

    AFAIK, there is no way to check the path of a PImage after it has been loaded. The best that you can do is store the path in a global variable:

    PImage img;  // Declare variable of type PImage
    final String imgFilename = "moonwalk.jpg"; // The path to the image
    
    void setup() {
      size(640, 360);
      img = loadImage(imgFilename);  // Load the image into the program  
    }
    
    void draw() {
      image(img, 0, 0);
      println(imgFilename);// Print the filename
    }
    

    Of course, this probably isn't anything like what you want. Depending on your needs, you could create a separate set of Strings that contain the filenames of corresponding PImages, or, perhaps, you could create a wrapper class that stores the PImage and the filename:

    SmartPImage img;  // Declare variable of type SmartPImage
    
    void setup() {
      size(640, 360);
      img = new SmartPImage("moonlight.jpg");  // Load the image into the program  
    }
    
    void draw() {
      image(img.img, 0, 0);
      println(img.filename);// Print the filename
    }
    
    class SmartPImage {
      PImage img;
      String filename;
    
      SmartPImage(String filename) {
        //Load the image and set the filename
        this.filename = filename;
        img = loadImage(filename);
      }
    }
    
  • edited March 2014

    Thanks Calsign, I had searched far and wide for something built in. I think your method could be useful.

    there is one typo in the class that stops it running: I changed "class SmartPIamge" to: "class SmartPImage"

    I guess the typo is proof you wrote that in your head, rather than my more mortal way of using the ide and trial and error :)

    cheers!

  • For anyone else watching: change "string" to "String" and "imgPath" to "imgFilename" to get your Calsign's first example to work.

  • Answer ✓

    Indeed, it appears that I am a bit careless today. This is what happens when I don't test the code before posting... it should be all fixed, now.

Sign In or Register to comment.