Home > Article > Backend Development > Creating art similar to million pounds worth of painting
Ever seen art and thought, "Yeah, I could make something similar to that." Well, you are not alone! If you search Damien Hirst's dot paintings online, a similar thought might cross your mind. Today we are going to test this theory using code.
We are trying to create art similar to popular artworks by artist Damien Hirst using Python code and the Turtle graphics library.
This blog post is inspired by Angela Yu's Python course.
We will need Python installed on our system (the latest version is better), Turtle graphics library (which comes pre-installed when you install Python), Colorgram library (which you have to install using pip), and your favorite IDE.
Hirst's art consists of a pattern of symmetrical color dots of the same size separated by consistent space.
It seems simple enough to mimic. So let's get to it.
Creating the Canvas:
#We import the Turtle library and give it an alias as t import turtle as t #We create an object of the Turtle class and name it bob bob = t.Turtle() #We create a screen that will be our canvas which our turtle (bob) #will draw the painting on screen = t.Screen() #To make the screen persistent we add the following line of code that #makes sure that the screen stays till we click on it. screen.exitonclick()
Extracting Colors:
You can install the colorgram library from this link.
#Import the colorgram library import colorgram #Make a list that will store the colors extracted from a sample #Damien Hirst painting. rgb_colors = [] #Extract the colors from the painting. You can choose how many colors #to extract, here 30 is chosen. colors = colorgram.extract('image.jpg', 30) #The output is a list of colorgram object of RGB values. We select #the RGB values from them. 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) #After we get the output, we can select the color list from the #terminal, store them into a variable like below, and comment the #above code. color_list = [(245, 243, 238), (202, 164, 110), (240, 245, 241), (149, 75, 50), (222, 201, 136), (53, 93, 123), (170, 154, 41), (138, 31, 20), (134, 163, 184), (197, 92, 73), (47, 121, 86), (73, 43, 35), (145, 178, 149), (14, 98, 70), (232, 176, 165), (160, 142, 158), (54, 45, 50), (101, 75, 77), (183, 205, 171), (36, 60, 74), (19, 86, 89), (82, 148, 129), (147, 17, 19), (27, 68, 102), (12, 70, 64), (107, 127, 153), (176, 192, 208), (168, 99, 102)]
Drawing the Patterns:
#Importing random module to randomly choose colors from color list import random # Since we are using RGB we set the colormode to 255 as there are 0 #to 255 values of RGB. t.colormode(255) #For better visualization we speed up the drawing process bob.speed("fastest") #by default our pen draws a line, we penup to avoid that bob.penup() # Going to an appropriate place in the canvas to draw all the dots in #the visible area bob.setheading(220) bob.forward(300) #Setting the heading in right direction to draw the dots bob.setheading(0) number_of_dots = 100 #drawing dots of size 20 at a distance of 50 from each other of a #random color from the color list. After every 10 dots we go the next #line for dots in range(1, number_of_dots + 1): bob.dot(20, random.choice(color_list)) bob.forward(50) if dots % 10 == 0: bob.setheading(90) bob.forward(50) bob.setheading(180) bob.forward(500) bob.setheading(0) #hiding our drawing pen bob.hideturtle()
Now there various Hirst's paintings to choose from and we can extract any number of colors from them so feel free to try out for yourself and see what kind of artwork you can come up with!
Overall it is a fun project and shows how easy it is to create something real from just the fundamentals of a programming language.
The above is the detailed content of Creating art similar to million pounds worth of painting. For more information, please follow other related articles on the PHP Chinese website!