art Coding

Pixel Sorting Using a Template

I created a picture like this…

Using these two separate images

This is a method known as pixel sorting which is done using python and can be used in multiple ways to get differing results. Such as, shifting the pixel in certain directions across the x or y axis. In this post I will be showing how to pixel sort using a black and white template image as I did above. I will be showing how this process works using the command prompt in Windows.

To see how to do some of the other pixel sorting you can look at the creator’s github for slightly less in depth instructions on the different methods

First thing first, you will need two images. One base image that we want to sort its pixels and one template image. The template needs to black and white for this method to work and based on my experience, it seems the template should be at least as big as the base image’s resolution. In the past it hasn’t seem to work for me if the template is too small. Of course, if you have a specific template that you really want to use that is the wrong size you could resize the image in a photo editor. For this example I’m going to use the two images below. My template is a good amount larger than the base image.

The next step is to install pixelsort in the command prompt by inputting:

pip install pixelsort
Installing pixelsort

In order to actually use our images we should navigate to their location in our computer. You can find the location of your images by searching your computer’s directory using:


Once you find the location in the directory of where your images are you can navigate to that location with:

cd <location name>
my images were in downloads so that is where I navigated to

If you need to look through multiple locations or accidentally navigated to the wrong direction you can navigate back to the parent location using:

cd ..

Now that we are in the directory that contains our two images all that’s left is to call upon pixelsort and mash em together. We do that with the following command:

python3 -m pixelsort <base image>.jpg -i file -f <template>.png -o <new file name>.jpg

The file type should not matter. The base image and template can be a png and jpg or both be a png or both a jpg. Additionally, the file name following “-o” is optional. Writing “-o <new file name>.png/jpg” tells pixelsort to save the final image as the name you write. If you don’t put this it will just name the image the current time.

Here’s our final product!

For the example that I just performed the template was slightly bigger than the base image so the pixels are not totally accurately sorted to the pattern of the template. Despite that, you can see how the process works and you can see there is a lot of room for creativity. So you can go ahead and mess around with what templates you use and their sizes to get different results.

This time you can see that the whole template is used on the base image but it is not as clear since I sized up a smaller image making the image resolution a bit lower quality. Once again just mess around with different templates and to try and get a product you like. That’s the fun in it.

Leave a Reply

Your email address will not be published. Required fields are marked *