Creating a Colorful Sketch of Messi using a Python Code

It's a colorful sketch of Lionel Messi's Face. In the bottom right corner, the text 'PySeek' is displayed.

Introduction

In this tutorial, we are going to learn how to draw the sketch of any human face using Python programming. To go with an example, I choose the iconic character, Lionel Messi. Here we will cover the whole topic by drawing the sketch of Lionel Messi using a Python Code.

To achieve this goal, we’ll use the Python sketchpy module, employing Python Turtle to draw Lionel Messi’s sketch based on the previously extracted coordinates of face elements (e.g. face outline, hair, eyes, ears, nose, etc.) from an image.

Every detail we’re about to explore is crucial, so stick around until the end, and let’s get Lionel Messi into the dynamic realm of Python programming!

How does the Program work?

Well, for the starter, we will need a good-quality photo of Lionel Messi. Since our program will give a cartoon outcome, it’s necessary to convert the chosen image into a cartoon. You can use many tools available online or, for an extra dose of Python fun, try out our Python application to effortlessly cartoonize an image (Convert an image to a cartoon using Python OpenCV).

Now, we need to find out every single x-y coordinates of the face elements (eyes, ears, hair, nose, nostril, chin, face outline, etc.) from the image. But, how do we achieve this feat, you wonder? I’ve employed a Python program to achieve this task (Take a peek at – How to Get Coordinates of Clicked Points on an Image).

But the adventure doesn’t end there. Now, we need to store the coordinates of each face element in several text files with a corresponding name. Once all the files are ready, Our trusty turtle, imported into the Python program, will gracefully follow these plotted points, bringing the face to life stroke by stroke.

Lastly, we need to choose different colors for different facial features to make the sketch more gorgeous. These colors will fill the sketch while the turtle will be drawing. If you’re curious about how the shades I’ve chosen from the image, there’s a handy online tool at your service — visit ImageResizer.com/color-picker. It offers to find out the RGB value of colors by selecting that (color) from an image.

Application screenshot displaying a cartoonized image of Lionel Messi with a zoom point focused on his face. Numeric values on the right indicate color information extraction. The top-left corner features the application name 'ImageResizer'.
Getting Color Information

I’ve generated a sum of 48 text files containing lists of coordinates, essential for drawing Messi’s face. The program relies on these files for proper execution. To obtain these files, please refer to the following sections.

Setting Up the Environment

Please ensure that Python is installed on your system. You can download it from the Python official site (https://www.python.org/). Now install a module called `sketchpy` using the following command.

pip install sketchpy

Download the zip file provided below and extract its contents. Next, create a Python program file named `messi.py` inside the unzipped folder. In this Python file, we will start writing our code.

Import the modules

Let’s start writing the code by importing the `canvas` from the `sketchpy` module.

from sketchpy import canvas

Create an object

Create an object of `canvas.sketch` class with the name `Turtle`.

Turtle = canvas.sketch(x_offset=290, y_offset=320)

Call the Draw Function

In this section, we will call the `draw_fn` function several times. Each call will sketch the outline of individual face elements, utilizing the coordinates presented in the text files.

Turtle.draw_fn("face_out", co=(233, 183, 151), mode=0)

Turtle.draw_fn("beard_out", co=(30, 25, 31), mode=0)

Turtle.draw_fn("chin1", co=(204, 139, 124), mode=0)
Turtle.draw_fn("chin2", co=(204, 139, 124), mode=0)

Turtle.draw_fn("lip_lower", co=(214, 125, 100), mode=0)
Turtle.draw_fn("lip_upper", co=(186, 30, 21), mode=0)

Turtle.draw_fn("nostril", co=(8, 15, 29), mode=0)
Turtle.draw_fn("nose_curve", co=(128, 69, 56), mode=0)
Turtle.draw_fn("right_eyebrow", co=(12, 16, 22), mode=0)
Turtle.draw_fn("left_eyebrow", co=(12, 16, 22), mode=0)

Turtle.draw_fn("noseline", co=(12, 16, 22), mode=0)

Turtle.draw_fn("eyelid", co=(13, 15, 23), mode=0)
Turtle.draw_fn("eye", co=(17, 12, 20), mode=0)
Turtle.draw_fn("sclera", co=(230, 231, 229), mode=0)
Turtle.draw_fn("eyeball", co=(15, 25, 15), mode=0)
Turtle.draw_fn("eyeball_centre", co=(230, 231, 229), mode=0)

Turtle.draw_fn("hair_outline", co=(12, 16, 25), mode=0)
Turtle.draw_fn("hair_shade1", co=(12, 16, 25), mode=0)
Turtle.draw_fn("hair_shade2", co=(61, 44, 52), mode=0)
Turtle.draw_fn("hair_shade3", co=(119, 64, 75), mode=0)
Turtle.draw_fn("hair_shade4", co=(119, 64, 75), mode=0)
Turtle.draw_fn("hair_shade5", co=(61, 44, 52), mode=0)
Turtle.draw_fn("hair_shade6", co=(119, 64, 75), mode=0)
Turtle.draw_fn("hair_shade7", co=(61, 44, 52), mode=0)
Turtle.draw_fn("hair_shade8", co=(61, 44, 52), mode=0)

Turtle.draw_fn("throat", co=(245, 207, 171), mode=0)
Turtle.draw_fn("throat_shade1", co=(236, 180, 153), mode=0)
Turtle.draw_fn("throat_shade2", co=(236, 180, 153), mode=0)

Turtle.draw_fn("ear_line1", co=(16, 10, 8), mode=0)
Turtle.draw_fn("ear_line2", co=(16, 10, 8), mode=0)
Turtle.draw_fn("ear_line3", co=(16, 10, 8), mode=0)
Turtle.draw_fn("ear_shade1", co=(212, 138, 124), mode=0)
Turtle.draw_fn("ear_shade2", co=(212, 138, 124), mode=0)
Turtle.draw_fn("ear_shade3", co=(212, 134, 122), mode=0)

Turtle.draw_fn("beard_shade1", co=(124, 77, 75), mode=0)
Turtle.draw_fn("beard_shade2", co=(127, 76, 76), mode=0)

Turtle.draw_fn("face_shade1", co=(209, 137, 122), mode=0)
Turtle.draw_fn("face_shade2", co=(208, 138, 119), mode=0)

Turtle.draw_fn("eye_shade1", co=(209, 143, 126), mode=0)
Turtle.draw_fn("eye_shade2", co=(209, 143, 126), mode=0)

Turtle.draw_fn("face_shade3", co=(245, 207, 171), mode=0)
Turtle.draw_fn("face_shade4", co=(240, 208, 169), mode=0)

Turtle.draw_fn("forhead_shade1", co=(202, 135, 119), mode=0)

Turtle.draw_fn("tshirt", co=(0, 30, 82), mode=0)
Turtle.draw_fn("tshirt_color1", co=(193, 36, 57), mode=0)
Turtle.draw_fn("tshirt_color2", co=(4, 31, 138), mode=0)
Turtle.draw_fn("tshirt_color3", co=(3, 35, 180), mode=0)

Turtle.draw_fn("end", retain=True, co=(230, 239, 234), mode=0)

In the code above, `draw_fn` takes the following parameters:

  • `file`: The path of the text file that contains the coordinates.
  • `mode`: Mode of drawing (1 – sketch with a line, 0 – fill with color).
  • `co`: The color of the line or fill.
  • `thickness` (default): Thickness of the line (In our case, we didn’t mention it in the program. By default it takes a value).
  • `retain` (default): Retain the image drawn after executing.

Output

A screenshot of an application window showing a sketch of Lionel Messi with the text 'Python Turtle Graphics' displayed on top of the window.
Output

Summary

In this tutorial, we covered How to draw a sketch of a human face using a Python program. Not only human faces, you can draw any drawable object by following the steps mentioned above. To cover this topic with an example, we go through the drawing of the sketch of Lionel Messi’s face using our Python Code.

The main program requires a total of 48 text files which contain the coordinates lists used in the program to draw each segment of Messi’s face. You can get those easily through the Download button provided before.

For more Cool Python Programs, visit our separate page packed with unique ideas. Below are a few examples to spark your interest:

Happy Sketching!

Share your love
Subhankar Rakshit
Subhankar Rakshit

Hey there! I’m Subhankar Rakshit, the brains behind PySeek. I’m a Post Graduate in Computer Science. PySeek is where I channel my love for Python programming and share it with the world through engaging and informative blogs.

Articles: 147