Python Resources

Updated on Aug 4 2024

Python In Other Contexts

Deborah R. Fowler



Python Pixel Art

Posted on Jan 22  2022
Updated on Jan 23  2022



PIL is the Python Imaging Library which when support discontinued in 2011, a fork became pillow as its replacement with Python 3.x support.

I am using PIL in the following example to create Pixel Art in turtle graphics out of any image. I created this due to a discussion after class one day about automating the process of making pixel art which has regained popularity recently. A few students each quarter have made pixel art in turtle, but this is the first time anyone asked how it could be automated so I thought I'd create an example.

Since python has a huge community I googled to see if anyone had done something similar. I did see many examples of things using many libraries - everything from numpy (scientific computing library) to matplotlib (visualization library) but I wanted to keep this as an example of using nested loops.

There are many ways to approach the solution, definitely the two main tasks to identify are:
Then using a nested loop, draw squares in a grid which is simply the quilt exercise and part of the lecture notes from class 4

Task 2 - how to convert color data to hex

PIL provided all the functionality I needed except for converting the color values over to hex. A google search revealed a promising result, however it ended up being a good reminder that you have to have some knowledge of code to determine whether the solutions you are using are valid. A quick test revealed the the code I found did not work with the turtle program. You also MUST credit your sources. After testing the code here and having turtle fail on a test image I was using I decided to write my own.

I wrote my own based on the description of hex numbers.There is a clear description of how rgb values can be mapped into a hex representation given at
https://www.developintelligence.com/blog/2017/02/rgb-to-hex-understanding-the-major-web-color-codes/

int is a function that returns only the whole number portion of the argument. This function given an rgb value will convert it to hex and return it suitable for turtle graphics use.


Task 1 - can be divided into two subtasks: getting the image data, getting it in the correct format per pixel

This section of code prompts the user for image and percentage of shrinkage, using the size function from PIL to resize.
Note that Image.open(filename) was used but .convert('RGB') was added so that the rgb color was a list of 3 not 4 values.
I choose size 10 to draw the squares, this could also be user defined.


PIL provided the ability to open the file and set up a list of values (used in the second case below). Next was to draw the pixel version with the correct colors.


For my classes, you are not allowed to use this for your Quilt exercise, however feel free to download the code below and experiment with your own images. It's a great example of nested loops!

demo.py