You are currently viewing Tutorial: Creating a Colorful Dot Painting with Python’s Turtle and Colorgram Modules

Tutorial: Creating a Colorful Dot Painting with Python’s Turtle and Colorgram Modules

  • Post author:
  • Post category:Python

Welcome to this step-by-step tutorial on creating a colorful dot painting using Python. In this guide, you’ll learn how to extract colors from an image and then use those colors to create a painting with the Turtle graphics module. This tutorial is designed for entry-level developers who are new to Python programming and graphics.

What You’ll Learn

  • How to use the colorgram module to extract colors from an image.
  • How to use the turtle module to draw colorful dots in a grid pattern.
  • How to combine these techniques to create a vibrant dot painting.

Prerequisites

Before you start, ensure you have the following:

  • Python 3.x installed on your computer. You can download it from python.org.
  • colorgram.py module installed. You can install it using pip: pip install colorgram.py
  • A sample image file named hirst.jpeg (or any other image you prefer) placed in the same directory as your script.

Code Breakdown

Step 1: Importing Modules

We start by importing the necessary modules:

import colorgram 
import turtle as t 
import random
  • colorgram: Used to extract colors from an image.
  • turtle: Used to draw shapes and create graphics.
  • random: Used to randomly select colors from the list.

Step 2: Extracting Colors from an Image

The extract_colors function extracts a specified number of colors from an image and returns them as a list of RGB tuples.

def extract_colors(image_path, num_colors):
    """
    Extract a specified number of colors from an image.

    Args:
        image_path (str): The path to the image file.
        num_colors (int): The number of colors to extract.

    Returns:
        list: A list of tuples representing RGB values of the extracted colors.
    """
    colors = colorgram.extract(image_path, num_colors)
    return [(color.rgb.r, color.rgb.g, color.rgb.b) for color in colors]

Arguments:

  • image_path: Path to your image file.
  • num_colors: Number of colors you want to extract.

Returns:

  • A list of RGB tuples, where each tuple represents a color.

Step 3: Creating the Dot Painting

The create_painting function uses the Turtle module to draw a grid of dots, each filled with a color from the extracted list.

def create_painting(colors, rows=10, cols=10, dot_size=20, spacing=50):
    """
    Create a painting of dots using the turtle module.

    Args:
        colors (list): A list of tuples representing RGB values.
        rows (int): Number of rows of dots.
        cols (int): Number of columns of dots.
        dot_size (int): The diameter of each dot.
        spacing (int): The distance between each dot.
    """
    t.colormode(255)
    painter = t.Turtle()
    painter.penup()
    painter.hideturtle()
    painter.speed("fastest")
    
    start_x, start_y = -250, -250
    painter.setpos(start_x, start_y)

    for row in range(rows):
        for col in range(cols):
            painter.dot(dot_size, random.choice(colors))
            painter.forward(spacing)
        painter.setpos(start_x, painter.ycor() + spacing)
  • Arguments:
    • colors: List of RGB tuples.
    • rows: Number of rows of dots.
    • cols: Number of columns of dots.
    • dot_size: Diameter of each dot.
    • spacing: Distance between dots.

Step 4: Running the program

The main function integrates the color extraction and painting creation.

def main():
    """
    Main function to extract colors and create a painting using turtle.
    """
    image_path = "hirst.jpeg"
    num_colors = 20
    colors = extract_colors(image_path, num_colors)
    
    create_painting(colors)

    screen = t.Screen()
    screen.exitonclick()

Steps:

  1. Set the path to the image file and number of colors.
  2. Extract colors from the image.
  3. Create a painting using the extracted colors.
  4. Keep the Turtle graphics window open until clicked.

Step 5: Running the code

To run the program:

  1. Save the code in a file named main.py.
  2. Open your terminal or command prompt.
  3. Navigate to the directory containing main.py and hirst.jpeg.
  4. Run the program using the following command: python main.py

You will see a window pop up displaying a colorful dot painting created by Turtle graphics.

Conclusion

Congratulations! You’ve learned how to extract colors from an image and use them to create a painting with Python’s Turtle graphics. This project demonstrates the power of combining image processing with creative graphics programming. Explore different images and color settings to create unique and beautiful paintings!

For the complete script and more examples, check out the Colorful Dot Painting Project on GitHub.

If you have any questions or need further assistance, feel free to ask. Happy coding!

Facebook Comments