Surface Size dependant on input Image dimensions

Hi everybody,

I am rewriting some code for P3. My aim is to fill up a PImage object with input image file data, selected with the new select-methods (such as https://processing.org/reference/selectInput_.html) and afterwards adjust the surface size depending on this PImages width and height.

This is an exampe code:

PImage input;

void setup() {
  size (200, 200);
  surface.setResizable(true);
  selectInput("Select an image to process", "imageSelected");
  surface.setSize(input.width, input.height);
}

void imageSelected(File selection) {
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
  } else {
    input = loadImage (selection.getAbsolutePath());
  }
}

It seems the interpreter can not wait until an input-file is picked (No clue about the selection window). It instantly neeeeeeds to run the setSize method - and crash (throws NullPointerException, because "input.width" is not available, because he wants it way to soon)! Is there another solution to my attempt? I think it is a decent desire, to set surface properties as a function of selected input. At least it is what my client desires ;)

Cheers!

Answers

  • _vk_vk
    edited November 2015

    Have you seen/tried this? I haven't but would start there...

    https://github.com/processing/processing/wiki/Changes-in-3.0#things-that-may-break-your-2x-sketches

    a teaser quote from there:

    ... But despair not! If you must change the size of your sketch, use...

  • Have you seen/tried this?

    Yes, I have. That's exactly what I am doing in the code I posted in my question. Not working properly.

  • _vk_vk
    edited November 2015

    Sorry I misread your post. :-S

  • edited November 2015 Answer ✓

    You could reszie the window when/after an image has been selected.

    PImage input;
    
    void setup() {
      size (200, 200);
      selectInput("Select an image to process", "imageSelected");
    }
    void draw(){
      if(input!=null){
        image(input, 0, 0);
      }
    }
    
    void imageSelected(File selection) {
      if (selection == null) {
        println("Window was closed or the user hit cancel.");
      } else {
        input = loadImage (selection.getAbsolutePath());
        surface.setSize(input.width, input.height);
      }
    }
    
  • _vk_vk
    edited November 2015 Answer ✓

    Maybe an while (input==null){selectInput...} would work. Halting the execution until you get an image, but it halts execution :( .

    Or even better do call surface.setSize() from callback imageSelected() if the image is not null.

  • edited January 2016

    Maybe an while (input == null) { selectInput() ... } would work.

    That'd create infinite selectInput() and crash the whole computer! >:)

    A better plan: *-:)

    PImage input;
    
    void setup() {
      size(200, 200);
      noLoop();
      selectInput("Select an image to process...", "imageSelected");
      while (input == null)  delay(100);
    }  
    

    Inside the callback:

    if (selection == null) {
      println("Window was closed or the user hit cancel.");
      selectInput("Select an image to process...", "imageSelected");
    } else { // ...
    
  • edited November 2015

    Thanks a lot for your help! I used the pattern by @benja - it works. Did not try out yours, @GoToLoop. Is it more clean? I mean, I got it "fixed somehow", but deep inside me I wanna be 100% aware about what exactly happens there and why it did "jump" over some code, when I expected it to halt the interpretation, while performing the input selection. Can someone explain? I guess afterwards this topic could be closed. Thank you, guys and girls! Great community, great software! :*

  • _vk_vk
    edited November 2015

    That'd create infinite selectgInput() and crash whole computer!

    ops sorry

  • Answer ✓

    but deep inside me I wanna be 100% aware about what exactly happens there and why it did "jump" over some code, when I expected it to halt the interpretation, while performing the input selection.

    selectInput() opens a file-chooser dialog, but the dialog is created in a different thread. So you code will not halt. And that's the reason why we use a callback-function ( i.e. imageSelected() ).
    If you want the code to wait, you would have to handle it yourself. That is what the example of @GoToLoop does.

  • Hey there,

    I just recently found a very simple solution for this in the ASDF Pixelsorting Sketch by Kim Asendorf (https://github.com/kimasendorf/ASDFPixelSort).

    He simply did it like this:

    
            void setup() {
              img = loadImage(imgFileName+"."+fileType);
              
              // use only numbers (not variables) for the size() command, Processing 3
              size(1, 1);
              
              // allow resize and update surface to image dimensions
              surface.setResizable(true);
              surface.setSize(img.width, img.height);
              
              // load image onto surface - scale to the available width,height for display
              image(img, 0, 0, width, height);
            }
    

    Hope it helps, cheers.

  • @isaakwit This is an old post. I doubt the owner is still around. In any case, currently you can use settings() to load an image and define the size of the sketch there:

    PImage img;
    void settings(){
      img=loadImage("sample.png");
      size(img.width,img.height);
    }
    
    void setup(){   }
    
    void draw(){  }
    

    Kf

  • Woah, didn't stumble across that function until now. Thanks a lot!

Sign In or Register to comment.