Categories
Python Turtle

How to Draw a Bicycle using Python Turtle

This tutorial covers how to draw a bicycle using Python Turtle. To draw with code in the Python programming language we can use the Python Turtle module which comes by default with the Python interpreter.

Drawing a bicycle using the Python Turtle module involves the following steps:

  1. Get an image of the bicycle you want to draw
  2. Get a grid for the image of the bicycle
  3. Combine the grid and the image of the bicycle into a single image
  4. Draw centre lines on the image of the bicycle
  5. Draw the bicycle using Python Turtle

Get an Image

A picture of a bicycle cannot be drawn with code. Rather, an icon should be used.

A suitable source of icon is The Noun Project. The landing page of the site is shown below:

The Noun Project Landing Page

Login or Join. This will enable you to download your icon. Type in the kind of image you want as shown below:

Icon Search

Pressing the search icon will bring out search items as shown below:

Search Results

Scroll down until you find a suitable icon then you click on it. This will take you to the screen shown below:

Selected Image

Click on the Get this icon green button. It will display the screen shown below:

Features

Select the Basic Download checkbox. The button will now turn green as shown below:

Basic Download

Click on the Continue button. This will show the screen shown below:

Final Download

Click on the PNG button. It will download an icon of image type PNG.

Get a Grid

To draw the icon, you need to use a grid. The grid allows you to get the coordinates of the points that you want to draw.

A good online tool to us is the Grid Drawing Tool. The landing page is shown below:

Grid Drawing Tool

Clicking on the start button will take you to the screen shown below:

Upload Screen

Click on the Choose file button. Navigate to the location of the image you downloaded on your machine.

The uploaded screen should look as shown below:

Uploaded Image

Clicking the Next button takes the user to the Rotate screen shown below:

Rotate Screen

Click the Next button to go to the Crop Screen which is shown below:

Crop Screen

Click on the Next button to get to the next screen.

Adjust Screen

Click on the Next button to get the grid. This is shown below:

Grid

Adjust the Grid dimensions to 20 and a line style of 1 pixels. Click the apply grid button as shown below:

Final Grid

Click on the Download button to get the grid image.

Combine the Grid and Image

Once you have your grid and image, you need to combine them into a single image.

To do this you can use any image editing software. However, in my case, I shall use Paint.NET.

Open the image in Paint.NET as shown below:

Paint.NET User Interface

Next import the grid as a layer. To do this, go to the Layers menu and click on Import From File… as shown below:

Import From File

When you import the grid, you will notice that the number of layers becomes two as shown below:

Two Layers

Now click on the Magic Wand. This will enable you to make the black areas transparent. The shortcut for the Magic Wand is S.

Click on a square in and press delete. This will give you the result shown below:

Upper Square

Increase the tolerance slightly until it spreads across the layer as shown below:

Increased Tolerance

Now press the delete button on your computer. It will give you the result shown below:

Deleted Spots

Now reduce the tolerance to 50% and delete all the dark spots. The final image will look as shown below:

Cleaned Image

Now we flatten the image. This will reduce the number of layers to one. The way to do this shown below:

Flatten

Press the Save button to save your image. You can now close Paint.NET. The image is now as shown below:

Modified Image

Draw Centre Lines

Drawing the centre lines is a matter of using the paint software on the computer.

Open the image in Microsoft Paint as shown below:

Microsoft Paint

Reduce the size of the image to 50%. This will enable you see the entire image as shown below:

Image Scaled Down

Under shapes in Microsoft Paint, click on line shape and set its colour to red. Also choose the maximum size.

Now draw the centre lines from one end of the image to the other. This will be as shown below:

Centre Lines

Press Ctrl + S to save the image and close Microsoft Paint. The final image will look as shown below:

Modified Image with Centre Lines

Draw the Bicycle

The bicycle will be drawn in stages. We will start with the wheels and draw the rest of the shape.

To start we shall import the Python Turtle module to our code, make use of the delay function and increase the pen size of our turtle.

Our code will look as shown below:

import turtle
turtle.delay(10)
turtle.pensize(20)

The resulting image is shown below:

The turtle is at the centre of the screen

Next we move to the right of the left bicycle and set the heading o the bicycle to 90 degrees. The coordinate of that position is (-3.5, -2). We shall use a scale factor of 25 so the coordinates are (-87.5, -50).

The code to do this is shown below:

# Move the turtle to the right position of the left tyre
turtle.penup()
turtle.setposition(-87.5, -50)
turtle.setheading(90)

The resulting image is shown below:

Move the turtle to Left Tyre

Next we draw the circle. The radius of the circle is 2.5. Scaled by 25, this is 62.5.

The code to draw the circle is shown below:

# Draw the left tyre
turtle.begin_fill()
turtle.circle(87.5)
turtle.end_fill()

The resulting image is shown below:

Left Tyre Drawn

Next we draw the right tyre. The turtle is moved to the right side of the right tyre and then, the circle is drawn. The position we are trying to get to is (8.5, 2). Scaled up by 25, this is (212.5, -50).

The code to do this is shown below:

# Move the turtle to the right position of the right tyre
turtle.penup()
turtle.setposition(212.5, -50)
turtle.setheading(90)

The resulting figure is shown below:

Set Heading for Right Tyre

Now draw the right tyre like you did the left tyre. The code to do this is shown below:

# Draw the right tyre
turtle.begin_fill()
turtle.circle(62.5)
turtle.end_fill()

The generated image is shown below:

Right Tyre Drawn

Now on to drawing the handles. We draw the line from (5.5, 0) to (3, 6). Scaled up by 25, this will be from (137.5, 0) to (75, 150).

The code to do this is shown below:

# Draw the handle
turtle.penup()
turtle.setposition(137.5, 0)
turtle.pendown()
turtle.setposition(75, 150)

The generated image is shown below:

Drawing the Handle Bar

Next we draw the handle. The handle is made up of two parts: a straight part and a semicircle. The straight part will be drawn first.

To draw the straight part, we draw a line from the position (3, 6) to (5, 6). Scaled by 25, this is (75, 150) to (125, 150).

The code to do this is shown below:

# Draw the straight part of the handle
turtle.setheading(0)
turtle.setposition(125, 150)

The generated image is shown below:

Straight Handle

Now we draw the semicircle. The code to do this is shown below:

# Draw the circular part of the handle
turtle.penup()
turtle.setposition(125, 100)
turtle.pendown()
turtle.circle(25, 180)

The generated image is shown below:

Handle Completed

Next is to draw the seat. The icon shows a seat tube and saddle. We draw the seat tube first.

The code to do this is shown below:

# Draw the seat tube
turtle.penup()
turtle.setposition(12.5, -50)
turtle.pendown()
turtle.setposition(-62.5, 137.5)

The generated image is shown below:

Seat Tube

Next we draw the seat. The key to drawing the seat is to set the heading of the turtle to 180 degrees. Next we draw a line from the current location to the position (-100, 137.5).

The code to do this is shown below:

# Draw the saddle
turtle.setheading(180)
turtle.setposition(-100, 137.5)

The generated image is shown below:

Saddle Drawn

To complete the drawing of the bicycle, we will display the diagram of a bicycle which is shown below:

Labelled Bicycle Diagram

The parts that are remaining to be drawn are the top tube, down tube, seat stay and chain stay.

To draw the top tube we draw a line from the position (100, 75) to (-37.5, 75). The code to do this is shown below:

# Draw the top tube
turtle.penup()
turtle.setposition(100, 75)
turtle.pendown()
turtle.setposition(-37.5, 75)

The generated image is shown below:

Top Tube Drawn

To draw the down tube we draw a line from the position (100, 75) to (12.5, -50) . The code to do this is shown below:

# Draw the down tube
turtle.penup()
turtle.setposition(100, 75)
turtle.pendown()
turtle.setposition(12.5, -50)

The generated image is shown below:

Down Tube Drawn

To draw the seat stay we draw a line from the position (-37.5, 75) to (-150, -50). The code to do this is shown below:

# Draw the seat stay
turtle.penup()
turtle.setposition(-37.5, 75)
turtle.pendown()
turtle.setposition(-150, -50)

The generated image is shown below:

Seat Stay Drawn

To draw the chain stay we draw a from position (12.5, -50) to (-150, -50). The code to do this is shown below:

# Draw the chain stay
turtle.penup()
turtle.setposition(12.5, -50)
turtle.pendown()
turtle.setposition(-150, -50)

The generated image is shown below:

Chain Seat Drawn

At the end of this tutorial, we have successfully drawn a bicycle using the Python Turtle module.

The code for this tutorial is available in its entirety as a GitHub Gist.

Learn Python in One Week

If you want a quick and easy introduction to the Python programming language, you should check out my fifth book: Learn Python in One Week on the Amazon store.