why doesn't PImage.resize work correctly?

hi,

could somebody explain why the code below doesn't work properly when source1.resize is not commented out? is it a bug or i'm simply not getting something?

PImage source1, img1;

void setup() {

  float screenf = 0.8;
  size(int(1920 * screenf), int(1080 * screenf), P3D);
  frameRate(60);
  background(50);

  source1 = loadImage("test_image20.png");
  source1.resize(width,height);  // DOES NOT WORK?

  img1 = createImage(width, height, ARGB);
  img1.copy(source1, 0, 0, width, height, 0, 0, width, height);


}

void draw() {
  image(img1, 0, 0);
}

if resize does not work for some reason, how to resize an image properly then so it can be copied?

Tagged:

Answers

  • What exactly do you mean when you say it doesn't work?

    Hint: what are the values of width and height when you're passing them into the resize() function? Print them out to be sure.

  • by saying it doesn't work, I mean the image (img1) does not show at all. if I comment out the source1.resize line, it does show ok (albeit just a part of the image (which is bigger).

    println(width); println(height); show: 1536 864

    but whatever dimension i try, it makes no difference. i also changed different renderers, to rule out renderer specifics. i'm sure i'm not aware of either certain functionality, feature or a bug.

    (i also tested bigger or smaller window sizes and bigger and smaller images).

  • currently my workaround is to adjust the arguments of img1.copy accordingly:

      source1 = loadImage("test_image20.png");
      //source1.resize(width,height);
      source2 = loadImage("test_image1.png");
      //source2.resize(width,height);
    
      img1 = createImage(width, height, ARGB);
      img1.copy(source1,0,0,1920,1080,0,0,width,height);
    

    in order to capture the whole image (which is 1920x1080) and copy it into img1, which is widthxheight. this presumes you know the dimension of pictures.

    it would be still good to know why resize "nulls" the img1.

  • edited December 2013
    source1 = loadImage("test_image20.png");
      source1.resize(width,height);  // DOES NOT WORK?
    

    Is by any chance, the width and height larger than the image's size ? I've had problems with resize() for this reason.

    Edit: I've tried your code with an image 1920x1080 in size and it works just fine.

  • Why do you copy source1 to img1 instead of just using source1?

  • edited December 2013

    In fact it's the copy command that doesn't work, your test_image20.png is most probably an RGB image, while the canvas you are trying to copy it to, is an ARGB image.

    You have 3 options:

    • Save your image in the ARGB format (with an alpha channel).

    • Create img1 with createImage(width, height, RGB).

    • Do what PhiLho wrote, don't use a second image and therefor ignore the color format (have a look at the following code).

    PImage source1;
    
    void setup() {
      float screenf = 0.8;
      size(int(1920 * screenf), int(1080 * screenf), P3D);
      frameRate(60);
      background(50);
      source1 = loadImage("test_image20.png");
      source1.resize(width,height);  // DOES NOT WORK? - it does!
    }
     
    void draw() {
      image(source1, 0, 0);
    }
  • this doesn't work for me either. Any ideas?

    /* @pjs preload="../georgios.jpg"; */
    PImage img;
    
    void setup() {
      size(800,800, P2D);
      colorMode(RGB,1);
      background(0);
      //frameRate(60);
      img = loadImage("../georgios.jpg");
      img.resize(100, 100); 
    }
    
    void draw() {
      background(0);
      image(img, 0, 0);
    }
    
Sign In or Register to comment.