How I Used Turtle Graphics to Create Dot Paintings

I remember the day when I first started to code with "Processing". Do you know it? Of course, you do. If not, get a glance at their website. "Processing" is the go-to software when you want to be called a coding artist. It gives you plenty of prebuilt tools to make your art.

But today's blog post is not about "Processing", it is about making art with code. Since I'm currently learning python, I asked myself if there are modules for python that let me create drawings as you can do with "Processing".

And yes, there is one! It's called Turtle Graphics. It's not as professional and versatile as Processing, but it's enough for the project.

So, what are we going to build today?

We are going to write a program that creates a dot painting. Similar to the one made by Damien Hirst, which he sold for $30,000. This is what my code generated.

Made by My Program

I'm not an art specialist. But I'm surprised by the amount of money Damien's client paid for that painting. I'm currently reading a book that explains how art is defined and priced. It's called "The 12 Million Stuffed Shark". When I've finished it, I will definitely make a blog post about that. But for now, let's leave the art analysis away, and let's talk about the actual coding. Something, I do understand ;).

As always, before I start any project, I first create a list of requirements that my project should fulfill.

First, the program asks the user for the file path of an image, from which he wants to extract the dominant colors. After that, the user can specify how big the gap between two dots should be. The program also asks for the background color and a linear function. By defining this function, you will get a diamond shape. From here on, the program becomes autonomous. Based on the inputs you gave, it prints your replica of Hirst's Dot painting. In the end, it takes a screenshot of the drawn image and saves it in your downloads folder.

That's it! Not that difficult to implement. You can replicate a $30,000 painting in under 5 seconds! Or you can create a completely new painting in the style of Pointillism. Here's another version that my program created. In this examples it uses the linear function f(x) = x.

Dot Painting Created by My Program

But what does the actual code look like? Here it is. The code should be self-explanatory.

import turtle as t
import random
import colorgram
import pyautogui

# get the location of the image
file_path = input("Enter file path of image: ")
user_func = input("Enter a linear function: ")
step_width = int(input("Enter a step width: "))

# extract colors form image and store it in a list
colors = colorgram.extract(file_path, 30)
rgb_colors = []
for color in colors:
    r = color.rgb.r
    g = color.rgb.g
    b = color.rgb.b

    new_color = (r, g, b)
    rgb_colors.append(new_color)

# initializing values for drawing
start_pos_x = 0
start_pos_y = 0
space = 50
dot_size = 20
columns = 10
rows = 10

# setting up my drawing robot
tim = t.Turtle()
tim.speed(0)
tim.setheading(225)
tim.penup()
tim.forward(200)
tim.setheading(0)
(start_pos_x, start_pos_y) = tim.position()
t.colormode(255)

# get the special function of the user
def f(x):
    f = eval(user_func)
    return f

# drawing function: draws dots
def draw_dotted_line(x, y):
    tim.goto(x, y + f(x))
    tim.pendown()
    tim.dot(dot_size, random.choice(rgb_colors))
    tim.penup()

# actual drawing happens here
for i in range(0, columns):
    for j in range(rows):
        draw_dotted_line(start_pos_x + step_width * j, start_pos_y + step_width * i)

# hides the turtle
tim.hideturtle()

# setting up screen size
screen = t.Screen()
screen.setup(width=1.0, height=1.0, startx=None, starty=None)

# takes a screenshot of the window
myscreenshot = pyautogui.screenshot()
myscreenshot.save('/Users/riccardofeingold/Downloads/screenshot.png')
# (optional) send you the image on to your phone

screen.exitonclick()
The Code of Pointillism

For more details on certain parts, you can always contact me via my website or social media.

If you liked this post, it would mean the world to me if you could share it with others who might be interested as well! Thanks for reading, and see you next time!