Drawing the Indian Flag Using Python: A Creative Coding Project

Introduction

Python programming language is widely known for its versatility and simplicity. It provides various libraries and modules that can be used to create stunning visualizations and graphics. In this article, we will explore how to draw the Indian flag using Python and some of its popular libraries.

To begin with, we need to import the necessary libraries. We will be using the Pillow library to create the image and the Matplotlib library to display it. So, make sure these libraries are installed on your system before you proceed.

from PIL import Image, ImageDraw
import matplotlib.pyplot as plt

Next, we need to create a blank image with the size of the Indian flag. The dimensions of the Indian flag are defined as a 2:3 ratio, where the width is twice the height. So, we can set the height as a constant value and calculate the width accordingly.

height = 300
width = height * 2
image = Image.new("RGB", (width, height), "#FFFFFF")
draw = ImageDraw.Draw(image)

Now, let's start drawing the Indian flag step by step. The flag consists of three horizontal stripes - saffron, white, and green - and a navy blue Ashoka Chakra at the center.

# Drawing the saffron stripe
draw.rectangle([(0, 0), (width, height // 3)], fill="#FF9933")

# Drawing the white stripe
draw.rectangle([(0, height // 3), (width, (2 * height) // 3)], fill="#FFFFFF")

# Drawing the green stripe
draw.rectangle([(0, (2 * height) // 3), (width, height)], fill="#138808")

# Drawing the Ashoka Chakra
center_x = width // 2
center_y = height // 2
radius = min(height, width) // 6
draw.ellipse([(center_x - radius, center_y - radius), (center_x + radius, center_y + radius)], fill="#000080", outline="#000080")

# Drawing the 24 spokes of the Ashoka Chakra
spoke_count = 24
angle = 360 / spoke_count
for i in range(spoke_count):
    x1 = center_x + radius * 0.9 * math.cos(math.radians(angle * i))
    y1 = center_y + radius * 0.9 * math.sin(math.radians(angle * i))
    x2 = center_x + radius * 0.99 * math.cos(math.radians(angle * i))
    y2 = center_y + radius * 0.99 * math.sin(math.radians(angle * i))
    draw.line([(x1, y1), (x2, y2)], fill="#FFFFFF", width=3)

Finally, let's display the image using the Matplotlib library.

plt.imshow(image)
plt.axis("off")
plt.show()

And that's it! You have successfully created and displayed the Indian flag using Python programming language. Feel free to modify the colors or customize the design according to your preferences.

Conclusion

Python offers a simple and effective way to create visual representations like the Indian flag. With the help of libraries like Pillow and Matplotlib, you can easily draw complex graphics and images. So, unleash your creativity and explore the endless possibilities of Python in the field of graphic design.


Similar Articles