User-Selected Images at setup() and during draw()

edited August 2014 in How To...

I've got this mostly working the way I want it. This proof-of-concept sketch allows a user to select an image on the fly via selectInput(), and the sketch window will automatically resize to fit the new image. I can make this happen in draw().

There are two hurdles I'd still like to overcome:

Any image loaded that is larger than the initial size() in setup() will not fully display, though its dimensions resize the frame appropriately. This can be worked around by making the initial size huge, but to me this is unseemly. A modest blank window initially would be less obtrusive. Alternatively the user should be able to select any starting image w/in setup(), and go on from there without limiting the size of subsequent images.

Which brings me to my second point: Is there any way to use selectInput() for images in setup()? A default image can be specified in the code, but that's not the same as launching the program with a file chooser dialog and going on from there.

Here's what I've got:

=================

PImage img;  
boolean fileSelected;
String path;

void setup() {
//  size(displayWidth, displayHeight);
  img = loadImage("/your/image/path/here.jpg");   
  img.resize(displayWidth, displayHeight); // Overload pixel array. Find workaround?
  size(img.width, img.height);
  image(img, 0, 0);
//  background(img);
// size(100, 100);                 // Try this to reproduce problem.
}

void draw() {

  if (fileSelected) {
    img = loadImage(path);
    frame.setSize(img.width, img.height);
    image(img, 0, 0, img.width, img.height);
  }
  fileSelected = false;
}

void fileSelected(File selection) {
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
  }
  
  else {
   path = selection.getAbsolutePath(); 
   fileSelected = true;
   println("User selected " + selection.getAbsolutePath());
  }
}

void keyPressed() {
 if (key == ' ') selectInput("Select a file to process:", "fileSelected");
}

=================

OSX 10.8.5 P5 2.2 Java mode

Answers

  • Answer ✓

    Based on an old post of mine, with some adaptations for the current version of Processing:

    PImage img; 
    boolean fileSelected;
    String path;
    java.awt.Insets insets;
    
    void setup() 
    {
      size(400, 400);
      frame.pack();
      insets = frame.getInsets();
      //  frame.setResizable(true); 
      selectImage();
    }
    
    void draw() 
    {
      if (fileSelected) 
      {
        img = loadImage(path);
        size(img.width, img.height);
    
        int ww = Math.max(width, MIN_WINDOW_WIDTH) + insets.left + insets.right;
        int wh = Math.max(height, MIN_WINDOW_HEIGHT) + insets.top + insets.bottom;
        frame.setSize(ww, wh);
        fileSelected = false;
      }
      if (img != null)
      {
        image(img, 0, 0);
      }
    }
    
    void fileSelected(File selection) 
    {
      if (selection != null) 
      {
        path = selection.getAbsolutePath();
        fileSelected = true;
        println("User selected " + selection.getAbsolutePath());
      }
    }
    
    void keyPressed() 
    {
      if (key == ' ') selectImage();
    }
    
    void selectImage()
    {
      selectInput("Select a file to process:", "fileSelected");
    }
    
  • Yes!!! Thank you!

    In case anyone else needs it, I added frame.toFront() to restore sketch window focus.

  • edited August 2014

    Update: I did this study as prep for another project, and I naively didn't anticipate problems moving from 2D to 3D. This does not work in P3D. Lesson hopefully learned about specifying and sticking with targeted environment.

    Update Update: OK, now I really wish I knew how to delete a comment. I had put P3D in size() in setup(), but not in draw(), and that was the problem. They have to be consistent. Duh.

Sign In or Register to comment.