Pixel Sorting in Python

RedXIII | April 13, 2021, 11:03 a.m.

Unlock your artistic talent with Pillow

As technology takes center stage in our lives, we turn to media to express ourselves. Knowing a programming language like Python can open many doors, including those that swing inward.

Coding is an opportunity to open the creative process. Projects like Google’s DevArt and Processing.org are making it easier for artists and engineers to innovate new, engaging experiences.

With Python, it’s possible to make exciting digital art using the power of pixels. With the Pillow library, Python can read and write image data, making it possible to create endless new works of art.

I’ll show you how to use Python to create a popular glitch art technique known as pixel sorting.


Pixel sorting is about rearranging the pixels in an image, usually by sorting rows or columns of image data. Remember, an image on the computer is made of thousands of pixels. Each pixel has three values, one for each of the three colors used to draw images on your monitor: red, green, and blue.

This ‘RGB’ data is combined to render the final color of every pixel you see.

The Pillow library gives us access to tools we can use to manipulate images. It offers a variety of image processing capabilities including, but not limited to, color enhancement and other image filters.

Before you can use Pillow, you’ll need to install it on your computer. It’s easiest to install the library using pip.

pip install Pillow

Once Pillow is installed, we can access the PIL image library with the following:

from PIL import Image

We’ll need a reference picture before we can start sorting. Keep in mind that the bigger the image, the longer the program will take to finish the sorting process. My image is around 700 pixels wide by 400 pixels tall.

Save the image in the same folder as the Python file you’re working with. I named mine pixel_sort.py.

Accessing the individual pixels of an image is fairly easy, as is storing the dimensions of the image.

This is all we need to start the sorting process. Image.load() returns a dictionary, or key/value pair, where each key points to an RBG value.

For instance, pixels[0,0] might be equal to (255, 0, 0) if that pixel’s value were bright red.

Using a nested for loop, we’ll traverse the rows of the image and then sort each row. There are many ways to sort pixels. Hue, luminosity, contrast, etc. These can all be used as criteria for conducting the sort.

For the sake of simplicity, we’ll focus on the overall value of each pixel. Since each pixel is made up of three colors (RGB), we can add them all up and get a single value.

For each row in the image, we’ll find the pixel with the highest overall value. This will serve as a breaking point for the sort, meaning we’ll only sort the array up to that pixel.

Doing this will yield some interesting artistic effects, but this is only one method of pixel sorting. It was inspired by Ken Asendorf, the inventor of the technique.

We’ll wrap the sorting code in it’s own function. This will get called for every row in the image, returning a sorted row.

The Pillow library allows us to create images from scratch using the new() method. We’ll need to specify the image mode (RGB) and provide the desired width and height.

new_img = Image.new(‘RGB’,(width, height))

Once the new image is created, we can add pixel data to it using the putdata() method. This method requires a Python list, so we’ll need to convert our pixel data into a list before it will work.

Display images using the show() method.

new_img.show()

Lastly, we can save the new image by using the save() method.

new_img.save(“sorted_red_rock.jpg”)

For example, Asendorf’s original code incorporated both horizontal and vertical sorting, and choosing other sorting methods can yield interesting results.

Feel free to modify this code, expanding on it’s potential and adding your own methods. Who knows, you might just make something beautiful.
Here’s the full code:

About Us

Learning at the speed of light.

We created Start Prism to help students learn programming. You can find exercises and recent tutorials below.

Topics Quizzes Tutorials

0 comments

Leave a comment