Revert diffusion effect

Mimicking a real diffusion model

Introduction

This script mimics the effect of a diffusion model. It takes an image as an input and progressively adds noise to it, until it becomes complete noise. Every frame is saved as a jpg file and can be used to create a video.

Features

  • Loads an image.

  • Modifies the RGB values of the image pixels based on a random algorithm.

  • Displays the modified image.

  • Saves the modified image after every frame update.

Usage

This script can be run in a Processing environment. A sample image (image.jpg) must be present in the same directory as the script.

Logic

The script starts by loading an image and setting the canvas size to 1024x1024. In the draw() function, a nested loop is used to iterate over the width and height of the image. In each iteration, a random number between 0 and 100 is generated, and if it is greater than 92, the RGB values of the current pixel are modified by a random offset value. The modified RGB values are then set for the current pixel. Finally, the modified image is displayed and saved with a filename that includes a frame count. The process is repeated for 1500 frames and then the script exits.

Dependencies

Known Issues

  • The script assumes that the image file (image.jpg) is present in the same directory as the script.

  • The script uses a hardcoded value of 1500 frames, and the script will exit after this number of iterations.

  • The offset value used to modify the RGB values is always the same for a given iteration, so the final output will be the same for each run of the script.

Script

void setup() {
  size(1024, 1024);
  img = loadImage("image.jpg");
}

void draw() {
  for (int i = 0; i < img.width; i++) {
    for (int j = 0; j < img.height; j++) {
      if (random(100) > 92) {
        int pixel = img.get(i, j);
        int r = int(red(pixel));
        int g = int(green(pixel));
        int b = int(blue(pixel));
        int offset = (int)(random(-40, 40));
        r = r - offset;
        g = g - offset;
        b = b - offset;
        img.set(i, j, color(min(255, max(0, r)), min(255, max(0, g)), min(255, max(0, b))));
      }
    }
  }
  image(img, 0, 0);
  saveFrame("revert_" + (1500 - frameCount) + ".jpg");
  if (frameCount == 1500) { 
    exit(); 
  }
}

Last updated