Deborah R. Fowler
Python Pixel Art
Posted on Jan 22 2022Updated 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:
- how do I get the color data from an image?
- how do I convert the color data to hex which turtle color requires
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.
- function below uses the hex value returned from myrgb_to_hex from the rgb color retrieved from the image using getpixel
- function below does the same thing as above but using
getdata. Each color is a subscripted value of the list.
Note that num is indexing incrementally as getdata
returns a long list, there is no sense of a two-dimensional list
(so if you come from C++ think vector array not two-dimensional
array). I use a nested loop to aid in drawing.
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