How to load images while an exported sketch is running?

edited March 2014 in How To...

Hi everyone! I'm new to this forum so I should present myself first: I'm Diogo, from Portugal, and I'm a graphic designer. I'm trying to make a sketch that I want to export (for Mac and Windows) to give to a few friends. The basic concept is that you can vectorize an image in a more graphic/abstract way. So I want them to be able to import their own images. I've seen some tips on loading images from a path or url. But I can't use an absolute path, since I'm going to share and export it... Any ideas? Thanks!

Answers

  • Answer ✓

    Welcome to the Processing Forum! You can have the user select the input file with selectInput().

  • Thank you! Exactly what I wanted. But I still have one question, if you please. I made the following code to only draw something after the image is loading. Works fine except one thing: Processing crashes when you exit the sketch...

    String filePath;
    PImage img;
    int x = 0;
    int y = 0;
    
    void setup() {
      size(800, 800);
    }
    
    void draw() {  
      if (x == 0) {
          selectInput("Select a file : ", "fileSelected");
          x++;
      }
      if (filePath != null && y == 0) {
          img = loadImage(filePath);
          image(img, 0, 0);
          y++;
      }
      if (filePath != null) {
          ellipse(mouseX, mouseY, 9, 9);
      }
    }
    
    void fileSelected(File selection) {
        if (selection == null) {
        } else {
        filePath = selection.getAbsolutePath();
        }
    }
    

    I was trying to use this solution, which is smarter, but it doesn't work.

  • Answer ✓

    I did a more simplified example. Check that out: :bz

    // forum.processing.org/two/discussion/3451/
    // how-to-load-images-while-an-exported-sketch-is-running
    
    PImage img;
    boolean hasRequested;
    
    void setup() {
      size(1280, 1024);
      noLoop();
      imageMode(CENTER);
    }
    
    void draw() {
      clear();
    
      if (hasRequested = !hasRequested)  selectInput("Select an image :", "selected");
      else if (img != null)              image(img, width>>1, height>>1);
    }
    
    void selected(File selection) {
      if (selection != null)  img = loadImage(selection.getAbsolutePath());
      else                    exit();
    
      if (img == null)        hasRequested = false;
    
      redraw();
    }
    
  • This is exactly what I wanted. Thank you both so much! (And thank you GoToLoop for the extra dancing Bee.) As soon as I get a beta version, I will share it with you. Thanks!

Sign In or Register to comment.