Random Collage Generator

Published by Tim on Tuesday September 28, 2021

Last modified on January 25th, 2024 at 14:07

Vimeo

By loading the video, you agree to Vimeos’s privacy policy.
Learn more

Load video

// Declare and initialize and array of images 
PImage[] images = new PImage[15];

void setup() {
  size(900, 900);

  // Slow down the whole thing
  frameRate(1);

  // Load images to the array
  for (int i = 0; i < images.length; i++) {
    // Load the specific image to the slot in the array
    images[i] = loadImage(i + ".png");

    // resize that image
    images[i].resize(2000, 0);

    // Apply a grayscale filter to the image
    images[i].filter(GRAY);
  }

  // center images and rectangles 
  rectMode(CENTER);
  imageMode(CENTER);
}

void draw() {
  background(#f1f1f1);


  // create a red ellipse at the center of the background
  fill(#ff0000);
  noStroke();
  ellipse(width/2, height/2, 700, 700);

  for (int i = 0; i < images.length; i++) {
    
    // Get the specific image
    PImage img = images[i];
    
    // set random x and y positions
    float x = random(width);
    float y = random(height);
    
    // decide how large the image shall be
    float scalar = random(0.1, 0.5);

    // place the image with a probability of 50%
    if (random(1) < 0.5) {
      
      // place the image
      push();
      translate(x, y);
      scale(scalar);
      image(img, 0, 0);
      pop();
    }
  }
}

Enjoying the content?

I put a lot of love and effort into developing content on Creative Coding. Since 2018, I have published 227 interviews, case studies, and tutorials, along with over 270 lessons in 17 online courses – and there's more to come! If you'd like to support my work and help keep this platform evolving, please consider supporting me on Patreon. Thank you very much!

Speaking Image

Monthly Newsletter

Fresh perspectives circling around Creative Coding, Design and Technology, every first Friday of the month, directly to your inbox.