Spatial Imprinting: A New Generative Art Algorithm

11/21/2023

A new generative art algorithm

In 2012 the artist Kim Asendorf introduced the generative art world to pixel sorting. This method, once a novel concept, has now firmly established itself in the toolkits of many creatives in the glitch and generative art communities. Let’s dive into the world of pixel sorting and explore how it has evolved into an even more intriguing process we call Spatial Imprinting.

The Process of Pixel Sorting

The pixel sorting algorithm starts with an image and sorts all or some of the pixels of each row and/or column according to a value that depends on the pixel’s color, for example it’s lightness, hue, or saturation. Here is an example from Kim Asendorf himself.

Mountains
Mountains

To understand how the pixel sorting algorithm works lets consider a single row of grayscale pixels. In Figure 1 below we represent each pixel as a square with a grayscale color so that we can visualize whats going on.

Figure 1: A row of unsorted grayscale pixels
Figure 1: A row of unsorted grayscale pixels

When we apply pixel sorting, we rearrange this row from the darkest shade to the lightest, resulting in a gradient of grayscale colors.

Figure 2: The same row of pixels, now neatly organized by lightness
Figure 2: The same row of pixels, now neatly organized by lightness

The pixel sorting algorithm applies this sorting to each row of the image. In the Mountains image above by Kim Asendorf he only sorts pixles in a specified range of lightness values, but for our purpose we need not worry about that, since for spatial imprinting we will want to sort the entire row or column.

Spatial Imprint: The Mapping Process

Here’s where the true enchantment lies: spatial imprinting. In this process, we track each pixel’s original position as we sort them, allowing us to “unsort” or revert the pixels to their initial arrangement.

To illustrate, let’s tag each pixel in Figure 1 with a number, from 0 to 15. After sorting, we can tell that the brightest pixel in Figure 2 originated from position 6 in Figure 1. This tracking mechanism is the key to spatial imprinting. Now, imagine applying this reversal not to the original image but to a second, completely different image. This is the essence of spatial imprinting.

Consider these two images: a vivid city street reflected in a storefront window, and a fishing boat at sea.

deco
City street scene
boat
Fishing boat

Through pixel sorting, we create a map that guides us from the color palette of the second image back to the setup of the original image. When we apply this map to the second image, the result is a hybrid—a blend of form from the first image and color from the second. Often some of the form of the second image will still appear in the result.

Let’s sort the city street image by its columns using lightness as our guide.

city stree sorted by columns
The city street image, sorted by columns

By itself, column sorting might not captivate the viewer. But the magic unfolds when we use the map to unsort the second image:

The city street scene, unsorted by columns with the boat palette
The city street scene, unsorted by columns with the boat palette

The result? A canvas where the foundational form of the city street is painted with the colors of the sea and the boat. The interplay of shape and hue produces an artwork that is familiar yet entirely new.

For an even more abstract rendition, we can venture into sorting by both rows and columns.

An abstract transformation through row and column unsorting
An abstract transformation through row and column unsorting

By also sorting the second image, we transform the algorithm into a filter that overlays the fundamental structure of the original image with a new aesthetic.

City street unsorted with sorted boat by lightness
City street unsorted with sorted boat by lightness

Another way we can get a more abstract image is to sort by hue instead of lighness. Here we swap images and use the city street as the palette for the fishing boat.

Fishing boat unsorted with city street by hue
Fishing boat unsorted with city street by hue

And here’s the intriguing outcome when we pre-sort the street image by lightness before unsorting the boat:

Fishing boat unsorted with city street by lightness sorted
Fishing boat unsorted with city street by lightness sorted

Applying the spatial imprinting algorithm to a single image can also produce interesting results. Of course we can’t apply the map to a sorted version of itself, that would result in no change. But we can simply apply it to the original image, which will result in a more abstract version of the original.

Fishing boat unsorted by rows
Fishing boat unsorted rows

You can see a few more examples of art generated by spatial imprint at spatial imprint. And if you want to try it yourself, you can find the rust code on github.