.resize() for every image in a PImage array.

Hello,

I have searched for a few topics on this but none of them had a simple and working solution or I couldn't manage. But it seems this has a simple solution yet I somehow couldn't do it. All I want is all these images in my array to be proportionally fit to a max width of 20 (which I believe that is resize(20,20) does) but I couldn't manage to call that function as the image is an array. I have tried all these three below the image() line but none of them worked cause it seems it locks them all to 20x20 pixel size. How can I manage this, thanks!

myImageArray[i].resize(20,0);
image(myImageArray[(int)random(5)].resize(20,0);
myImageArray.resize(20,0);

Here is my code;

PImage[] myImageArray = new PImage[20];
float px;
float py; 

void setup() {
  size(600, 600,P3D);
  frameRate(30);
  background(0);

  for (int i=0; i<myImageArray.length; i++) {
   myImageArray[i] = loadImage( "frag_" + i + ".jpg");
  }
}

void draw() {
  background(0); // For glitch effect
  pushMatrix();
  translate(px, py);
  py+=1; // Glitch skip
  for (int i=0; i<width; i+=25) {
    for (int j=-3000; j<3000; j+=23) {
    //for (int j=0; j<3000; j+=25) { // For glitch effect
     image(myImageArray[(int)random(5)], i, j, 20, 20);
    }
  } 
  popMatrix(); 
}
Tagged:

Answers

  • For speed sake don’t use image with 5 parameters just with 3

    Instead use resize in setup

    I think the syntax is

    image[i] = image[i].resize(20,20);

    Usd 0 instead of 20 to make it proportional

    20,0

  • Thanks, I have tried;

    myImageArray[i] = myImageArray[i].resize(20,0);
    

    in the for loop in the setup but it gave type mismatch, void does not match with processing.core.PImage error. Wherever I call .resize function I get various errors :)

  • edited January 2018

    https://Processing.org/reference/PImage_resize_.html
    https://Processing.org/reference/void.html

    for (int i = 0; i < myImageArray.length; ++i)
      ( myImageArray[i] = loadImage("frag_" + i + ".jpg") ).resize(20, 0);
    
  • My code at the moment looks like this, I have added that .resize() but still all the images are 20,20 - what might I be doing wrong?

    PImage[] myImageArray = new PImage[5];
    float px;
    float py; 
    
    void setup() {
    
      size(600, 600);
      frameRate(30);
      background(0);
    
      for (int i = 0; i < myImageArray.length; ++i) {
        (myImageArray[i] = loadImage("frag_" + i + ".jpg")).resize(20, 0);
      }
    }
    
    void draw() {
    
      background(0); // For glitch effect
      pushMatrix();
      translate(px, py);
      py+=1; // Glitch skip
      for (int i=0; i<width; i+=35) {
        for (int j=-3000; j<3000; j+=30) {
        //for (int j=0; j<3000; j+=25) { // For glitch effect
         image(myImageArray[(int)random(5)], i, j, 20, 20);
        }
      } 
      popMatrix(); 
    }
    
  • Answer ✓

    IN line 25 please remove the two last parameters

    20,20

  • Yes, finally! Thanks.

  • this

    for (int i = 0; i < myImageArray.length; ++i) {
        (myImageArray[i] = loadImage("frag_" + i + ".jpg")).resize(20, 0);
      }
    

    same as

    for (int i = 0; i < myImageArray.length; i++) {
        myImageArray[i] = loadImage("frag_" + i + ".jpg");
        myImageArray[i].resize(20, 0);
      }
    
  • Actually I have tried those as well but didn't remove the 20,20 in draw... I thought this resize would override the arguments in image but it clearly doesn't :)

  • Answer ✓
    • Method PImage::image() is overloaded to resize() w/ 5 parameters.
    • The diff. is that PImage::resize() modifies the PImage itself.
    • While PImage::image() w/ 5 parameters that is temporary.
    • That is, rather than resize() itself, another PImage clone is created on-the-fly, resized to the specified 4th & 5th passed arguments,and then displayed.
  • ...and temporary resizing is almost always a bad idea (terrible for performance). With very few exceptions, don't resize in draw(), and don't use the 5-parameter image() unless it is a once-only call.

  • Many thanks for the bonus tips, this really helped a lot.

Sign In or Register to comment.