Drawing the Hirst spot painting with python

You might have heard of the famous Damien Hirst spot paintings that sold for a huge amount of money, what if you could make them using Python on your local machine.

Requirements

You need to have Python installed in your machine, an IDE(Integrated Development Environment) such as Pycharm or the default IDE that comes with Python. You can install Python for your operating system from www.python.org and pycharm IDE from PyCharm: the Python IDE for Professional Developers by JetBrains

Basic knowledge of python

The modules need are turtle, random and colorgram

turtle and random are preinstalled while you can install colorgram by running this command pip install colorgram.py in your command line

What are the modules used for?

Turtle is a python GUI(graphical user interface), for more details and how to use, visit the python turtle page at [turtle — Turtle graphics — Python 3.11.4 documentation]

Random is a python module that can be used to pick randomly from a range of numbers, integer, list etc. Read more on Python | Random Module | Codecademy

colorgram is a python module that is used to extract colors from an image in rgb or hsl format. visit colorgram.py · PyPI for documentation

Writing the code

Open up the IDE of your choice

import turtle, random, and colorgram

import turtle 
import colorgram
import random

set up the turtle and screen object

# create a turtle object
tim = turtle.Turtle()
# create  the turtle screen
screen = turtle.Screen()

The .exitonclick() Screen method will be called to prevent the screen from disappearing before we get to see what is going on.

# all your code should be written above this method call
screen.exitonclick()

The turtle object will be given some functionalities to suit the project we are building

# This will increase the speed of the turtle as it moves 
tim.speed('fastest')
# the turtle object should an arrowhead like shape on screen, and the method below will hide it.
tim.hideturtle()
# the turtle's default position is the middle of the wind, w'll move it to the bottom left corner of the window
tim.setheading(225)
tim.forward(320)
# set the direction to right
tim.setheading(0)
# This is going to prevent the turtle from leaving traces as it draws the spot painting
tim.penup()
# changes the default colormode to 255
screen.colormode(255)

The next step is getting the needed colors for the project, to this effect we will extract the colors in any of the damien hirst paintings to make ours, you can download any hirst image for this purpose

# this extracts 20 colors from the image and return a list of colorgram objects
colors = colorgram.extract('image path', 20)
list_of_colors = []
rgb = []
# This loops through the object list to get all the rgb color numbers
for color in colors:
    r = color.rgb.r
    b = color.rgb.b
    g = color.rgb.g
    new_color = (r, g, b)
    list_of_colors.append(new_color)

Finally we are going to draw our hirst painting .

# this variable holds the number of dots in our painting
no_of_dots = 100

# for the number of the dots
for dot_count in range(1, no_of_dots+1):
# draw a dot of size 40, and pick a random color from the list of extracted colors
    tim.dot(40, random.choice(list_of_colors))
#  move it right by 60 pixels
    tim.forward(60)
# for every 10 dots, turn the turtle and start all over.
    if dot_count % 10 == 0:
        tim.left(90)
        tim.forward(50)
        tim.left(90)
        tim.forward(590)
        tim.setheading(0)

Conclusion

Hope you enjoyed building this with me, thanks for following true.

I would love to hear from you.

0
Subscribe to my newsletter

Read articles from KELVIN CHI-JEKWU UGWU directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

KELVIN CHI-JEKWU UGWU
KELVIN CHI-JEKWU UGWU