Search code examples
image-processingprocessingimage-resizing

How can I randomize Image size


Thank you to the people who previously helped me, I have managed to work a lot on my generative business cards assignment.

I want to randomly resize 9 images in processing but can't seem to find a good example on the internet on how to do it. The size of the images is 850x550 which is also the background size.

Does anyone know a good and easy to follow tutorial? or could give me an example?


Solution

  • say you have stored an image in a PImage object, image you can generate two random integers for the img_width and img_height of the image and then resize() the image using resize() method

    int img_width = foor(random(min_value, max_value));
    int img_height = floor(random(min_value, max_value));
        
    image.resize(img_width, img_height); //this simple code resizes the image to any dimension
    

    or if you want to keep the same aspect ratio, then you can use this approach

    //first set either of width or height to a random value
    int img_width = floor(random(min_value, max_value));
        
    //then proportionally calculate the other dimension of the image
    float ratio = (float) image.width/image.height;
    int img_height = floor(img_width/ratio);
        
    image.resize(img_width, img_height);
    

    You can check this out YouTube playlist for some tutorials of image processing.