GET OFF YOUR PHONE!

Heads up!

Look at the sun.

Appreciate the light.

Come back when you are on a Lap or Desktop.

(This page would also drain your mobile data, trust me)
F a r n F r e u n d/ │ F E E D/

Close Encounters

09.04.25

A recent Pixel toy I've built in Processing. Another script as a little prayer to the godnumber, the number of all possible images.

This prayer here is a simple one:

A small part of an image is pseudorandomly chosen. Then the pixels inside of that frame are visually connected to all the other pixels holding the exact same RGB value.

In other words:

PImage canvas;
IntList regionColors = new IntList();
int posX, posY, size;
int accX, accY;
int index;
void setup() {
  size(1280, 720);
  //load the source image and resize it to the window size
  canvas = loadImage("trail.jpg");
  canvas.resize(1280, 720);
  canvas.loadPixels();

  //Set the position and movement direction and speed of the frame randomly
  accX = int(random(-5, 5));
  accY = int(random(-5, 5));
  posX = int(random(0, canvas.width - size));
  posY = int(random(0, canvas.height - size));

  index = 0;
  size = 10; // size of the square shaped frame
}

void draw() {

  image(canvas, 0, 0);


  // Collect all colors from the chosen region and store 
  // pixel positions and their color in a Hashmap
  HashMap<Integer, ArrayList<int[]>> regionPixels = new HashMap<>();
  for (int i = posX; i < posX + size; i++) {
    for (int j = posY; j < posY + size; j++) {
      int loc = i + j * canvas.width;
      int c = canvas.pixels[loc];

      // Store pixel position (x, y) for each color
      if (!regionPixels.containsKey(c)) {
        regionPixels.put(c, new ArrayList<int[]>());
      }
      regionPixels.get(c).add(new int[]{i, j});
    }
  }

  // Check all other pixels for matches
  for (int z = 0; z < canvas.pixels.length; z++) {
    int x = z % canvas.width;
    int y = z / canvas.width;
    // Skip the area inside the frame
    if (x >= posX && x < posX + size && y >= posY && y < posY + size) {
      continue;
    }
    int c = canvas.pixels[z];
    // Check if the color matches any from the frame
    if (regionPixels.containsKey(c)) {

      // Draw lines between the matched pixels their RGB twins
      for (int[] FramePixelPosition : regionPixels.get(c)) {
        stroke(c); // Line color matches the color in question
        line(FramePixelPosition[0], FramePixelPosition[1], x, y);
      }
    }

  }

  // Draw a rectangle around the selected region
  noFill();
  stroke(255);
  rect(posX, posY, size, size);
  rect(posX + canvas.width, posY, size, size);

  // Move the Frame and change its direction and speed if its the windows bounds
  posY+=accY;
  if (posY>= canvas.height-size) {
    accY = int(random(-5, -1));
    posY+=accY;
  }
  if (posY<= 0+size) {
    accY = int(random(1, 5));
    posY+=accY;
  };

  posX+=accX;
  if (posX>= canvas.width-size) {
    accX = int(random(-5, -1));
    posX+=accX;
  }

  if (posX<= 0+size) {
    accX = int(random(1, 5));
    posX+=accX;
  }
// optional: save the frames and stop the program after 240 frames
  saveFrame("###.jpg");
  index++;
  if (index== 240) exit();
}

Once could also change the code so that no lines are drawn but all the matching pixels are highlighted in let's say magenta:

[...]  
for (int z = 0; z < canvas.pixels.length; z++) {
    int x = z % canvas.width;
    int y = z / canvas.width;
    // Skip the area inside the frame
    if (x >= posX && x < posX + size && y >= posY && y < posY + size) {
      continue;
    }
    int locMatch = x + y * canvas.width;
    int c = canvas.pixels[z];

    // Check if the color matches any from the frame
    if (regionPixels.containsKey(c)) {
      // Hightlight the Pixels of matching color 
      for (int[] FramePixelPosition : regionPixels.get(c)) {
          pixels[locMatch]= color(255,0,255);
      }
    }
    else{
      pixels[locMatch]= canvas.pixels[z];
    }
  }
  updatePixels();
[...]

A neat side effect here is that trough highlighting, the blocky artifacts of compression can emerge.

It's a fun little tool to create new images but it's awfully slow. Especially with greyscale images, that have a lot of more matching pixels naturally. So a live installation might be difficult out of the box, but I might port it to Python and see if that will speed things up. However I got an Idea for a little video work already cooking.

Anyway have some more rendered material:

While this deals with only the pixels inside a single image it is part of a bigger project that is yet again a sub-project of my God number project. Eventually I want to find two different images, that while depiciting different motifs, still hold the same exact pixels, just in a different order along the grid. Twin Pics you could say.

Im sure someone already tried this and perhaps succeded. So any pointers are appreciated.

But somewhere in those numbers lies the solution. I just gotta study them long enough.

Related:

Twin Pics [Rebuilder]

Tags

💬