resize

edited September 2016 in Questions about Code

If I use resize on a loaded image, does it change the resize the actual image in memory?

memimg = loadImage("c:dirtscan.png"); 
memimg.resize(480,480);              // scale to fit screen 480x480
sclimg=memimg;
image(sclimg,imgstartcol,imgstartrow);

So now memimage.height and memimage.width =480?

If I want to get back to the full scale raw image in memory to execute image processing on I have to reload the image each time after i resize it?

Is that correct?

Tagged:

Answers

  • edited September 2016

    What happened when you put together a little test program to find out yourself?

    The resize() function is destructive. Once you resize an image, you can't get its exact original without reloading the file again.

    Other than creating a little example program (which should always be your first step), we can look at the source for PImage to be sure:

    public void resize(int w, int h) {  // ignore
        if (w <= 0 && h <= 0) {
          throw new IllegalArgumentException("width or height must be > 0 for resize");
        }
    
        if (w == 0) {  // Use height to determine relative size
          float diff = (float) h / (float) height;
          w = (int) (width * diff);
        } else if (h == 0) {  // Use the width to determine relative size
          float diff = (float) w / (float) width;
          h = (int) (height * diff);
        }
    
        BufferedImage img =
          shrinkImage((BufferedImage) getNative(), w*pixelDensity, h*pixelDensity);
    
        PImage temp = new PImage(img);
        this.pixelWidth = temp.width;
        this.pixelHeight = temp.height;
    
        // Get the resized pixel array
        this.pixels = temp.pixels;
    
        this.width = pixelWidth / pixelDensity;
        this.height = pixelHeight / pixelDensity;
    
        // Mark the pixels array as altered
        updatePixels();
      }
    

    From this, you can see that the internal pixels, width, and height variables are changed, and no copies are kept around.

  • edited September 2016 Answer ✓

    https://forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text

    Get a clone of the original PImage w/ get() before using resize(): *-:)

    1. https://Processing.org/reference/PImage_get_.html
    2. https://Processing.org/reference/PImage_resize_.html

    PImage img, ori;
    
    void setup() {
      ori = ( img = loadImage("dirtscan.png") ).get();
      img.resize(480,480);
    }
    
Sign In or Register to comment.