Last modified: Apr 21, 2025 By Alexander Williams
Python Drawing Shapes on Images Guide
Drawing shapes on images is a common task in image processing. Python offers powerful libraries like OpenCV and PIL for this purpose.
This guide will show you how to draw shapes on images. You will learn to draw lines, rectangles, circles, and text.
Table Of Contents
Prerequisites
Before starting, ensure you have Python installed. You also need OpenCV and PIL libraries.
Install them using pip:
pip install opencv-python pillow
Drawing Shapes with OpenCV
OpenCV is a popular library for image processing. It provides functions to draw shapes easily.
Drawing a Line
Use the cv2.line()
function to draw a line. Specify the image, start, end, color, and thickness.
import cv2
# Load an image
image = cv2.imread('image.jpg')
# Draw a line
cv2.line(image, (50, 50), (200, 50), (255, 0, 0), 2)
# Save the image
cv2.imwrite('image_with_line.jpg', image)
The code draws a blue line from (50,50) to (200,50). The color is in BGR format.
Drawing a Rectangle
Use the cv2.rectangle()
function to draw a rectangle. Specify the top-left and bottom-right corners.
# Draw a rectangle
cv2.rectangle(image, (50, 50), (200, 200), (0, 255, 0), 2)
# Save the image
cv2.imwrite('image_with_rectangle.jpg', image)
The code draws a green rectangle. The last parameter is the thickness.
Drawing a Circle
Use the cv2.circle()
function to draw a circle. Specify the center, radius, and color.
# Draw a circle
cv2.circle(image, (150, 150), 50, (0, 0, 255), 2)
# Save the image
cv2.imwrite('image_with_circle.jpg', image)
The code draws a red circle with radius 50. The center is at (150,150).
Adding Text
Use the cv2.putText()
function to add text. Specify the text, position, font, and size.
# Add text
cv2.putText(image, 'Hello', (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
# Save the image
cv2.imwrite('image_with_text.jpg', image)
The code adds white text at (50,50). The font is Hershey Simplex.
Drawing Shapes with PIL
PIL (Python Imaging Library) is another option. It is simpler but less powerful than OpenCV.
Drawing a Line
Use the ImageDraw.Draw.line()
method to draw a line. Specify the start, end, and color.
from PIL import Image, ImageDraw
# Load an image
image = Image.open('image.jpg')
draw = ImageDraw.Draw(image)
# Draw a line
draw.line([(50, 50), (200, 50)], fill='blue', width=2)
# Save the image
image.save('image_with_line_pil.jpg')
The code draws a blue line. The color is specified as a string.
Drawing a Rectangle
Use the ImageDraw.Draw.rectangle()
method to draw a rectangle. Specify the corners.
# Draw a rectangle
draw.rectangle([(50, 50), (200, 200)], outline='green', width=2)
# Save the image
image.save('image_with_rectangle_pil.jpg')
The code draws a green rectangle. The outline parameter sets the color.
Drawing a Circle
Use the ImageDraw.Draw.ellipse()
method to draw a circle. Specify the bounding box.
# Draw a circle
draw.ellipse([(100, 100), (200, 200)], outline='red', width=2)
# Save the image
image.save('image_with_circle_pil.jpg')
The code draws a red circle. The bounding box defines the size.
Adding Text
Use the ImageDraw.Draw.text()
method to add text. Specify the position and text.
# Add text
draw.text((50, 50), 'Hello', fill='white')
# Save the image
image.save('image_with_text_pil.jpg')
The code adds white text at (50,50). The fill parameter sets the color.
Conclusion
Drawing shapes on images in Python is easy with OpenCV and PIL. Both libraries offer simple functions for this task.
OpenCV is more powerful and suitable for complex tasks. PIL is simpler and good for basic operations.
For more advanced image processing, check our Python Image Processing Guide.
If you need to analyze images, read our Python Image Analysis Guide.
For resizing images, see our Python Image Rescaling Guide.