Producing Artwork in Python Inspired by Hirst's Million Dollar Landscape Painting

I've been to an art museum and wondered how a seemingly ordinary piece of art can be so famous or sold for such a high price? Or can works of art that seem so simple to make with such basic techniques be so highly regarded and attract a large crowd of bidders? Such is the case with Hirst's landscape paintings.
Damien Hirst is an English artist known for his contemporary art on life, death, and beauty. His famous landscape paintings consist of full circles of different colors arranged in a grid pattern with a light colored background. These paintings, although plain and simple, have been sold at auctions for up to a million dollars!
In this article, we'll use Python's colorgram and the turtle module to create spot paintings, drawing inspiration from Hirst's color palette and techniques. This is a beginner-friendly Python course but requires a basic knowledge of Python basics, as well as creating objects and using methods, and importing and using Python modules. In this fun and interesting Python tutorial, we will learn to apply the concept of OOP in programming and learn how to achieve our purpose using modules and a few lines of code.
Let's awaken the artist in us while learning Python coding!
Spot Paintings
Hirst's spot paintings consist of small areas of various colors aligned in a grid pattern. The paintings differ in their number of dots, and the color palette used in these paintings. We'll use Python's colorgram module to extract the color palette from these highly regarded graphics, and then use Python's turtle module to draw these spots in a grid format. We can specify the number of dots on our canvas, and the distance between them, and the color scheme used.
For reference, consider checking out these drawings via this link.
Brings Out Colors
The first thing is to extract a color palette from one of Hirst's paintings. We'll download the diagram to the same directory as the Python file we're writing our program in. But first, let's install the colorgram module by typing the following command lines in the terminal:
pip install colorgram.py
Next, we will import colorgram module in our code and use the extract function, while specifying the image file and the number of colors we want to extract from the reference image as arguments to extract work. Here is an image file that I downloaded as “ref.jpg” and used to define the color palette for my Python-generated scene drawing.
import colorgram
colors = colorgram.extract("ref.jpg",20)
print(colors)
If we use the above lines of code, it will print the 20 colors present in the reference image:
Note that the colors extracted above are in a different format than the ones compatible with the turtle module. Now we will turn these colors into colors that match the turtle.
Creating a Color List
First, we will create an empty list then add the extracted colors one by one after proper formatting to RGB format. We will enter the r, g, and b values of the extracted colors and store them in our created array with the appropriate formatting. For this purpose, we will use the for loop:
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)
print(rgb_colors)

As seen in the image above, the RGB colors are properly formatted and can now be used in advance to randomly select dot colors.
Importing Turtle and Configuring Basic Settings
Now, we will import the Python turtle module to create our artwork. We will explain the basic settings of our turtle. The first is colormode()which will determine how we will use colors in our code beforehand, between 0 and 1, or between 0 and 255. Check out more about the job through this link.
import turtle
turtle.colormode(255)
tim = Turtle()
tim.shape("turtle")
tim.color("coral")
Since we are using RGB colors in their 0-255 format, we added 255 as an argument. In addition, we created a turtle object from Turtle class in Python's turtle module and named it tim, as well as customizing its shape and color. We will use this turtle in the foreground as our drawing tool.
Stopping our Turtle
Now that we have created the turtle object, let's visualize it with the help of the screen object.
from turtle import Screen
screen = Screen()
screen.exitonclick()

As seen in the image above, the turtle is placed in the middle of the screen. We need the turtle to be in one corner to start creating dots in the line. We can choose the bottom left corner as the starting point, and the turtle moves from left to right and up while drawing dots.
We can achieve this with the turtle function setheading() which takes an angle as a parameter to set the direction of the turtle, and after some trial and error, we know that this can be done by setting the angle to 225. We will then move forward by a certain distance, say 300. This is how our turtle is now at the bottom left:

We'll also set the turtle's direction to 0, so we can start with our dot drawing.

Dot Painting
Now, with our turtle's path set, we'll start drawing the dots. We'll use the turtle dot method, which takes a dot size and color to fill. We will achieve this dot plot by using a loop, keeping in mind the total number of dots we want. If we want a 10 by 10 grid of dots, the total number of dots will be 100. After every 10 dots, the turtle must move up the line and continue drawing dots. We will use modulo functionality for this purpose. In addition, we will import and use the random module to select the color of each dot.
We will use the forward again setheading ways to for loop to create dots forward, then upward:
import random
number_of_dots = 100
for dot_count in range(1, number_of_dots + 1):
tim.dot(20, random.choice(rgb_colors))
tim.forward(50)
if dot_count % 10 == 0:
tim.setheading(90)
tim.forward(50)
tim.setheading(180)
tim.forward(500)
tim.setheading(0)

Hide the Turtle and Line
Once our dots are created, we need to hide the turtle and lines. We can do this easily with hideturtle() again penup() methods.
tim.penup()
tim.hideturtle()
This is what the final output will look like once we have executed the above lines of code:

The conclusion
As can be seen above, we have successfully created a beautiful work of art following Hirst's dot technique. Python's colorgram module allowed us to generate a beautiful color palette, and the turtle module helped us create artwork using that palette. This is a basic example of how such beautiful pieces can be created using code, and what happens when art and programming meet.



