Pillow Imagedraw Module
π
2026-06-22 | π Pillow
Pillow ImageDraw Module | Novice Tutorial
ImageDraw is a module in the Python image processing library Pillow (a fork of PIL) used for drawing various shapes and text on images.
ImageDraw provides rich drawing functionalities, including lines, rectangles, circles, polygons, text, etc., suitable for image annotation, generating charts, or simple graphic design.
The main features of the `ImageDraw` module include:
* Supports drawing on existing images
* Can create entirely new blank canvases
* Provides multiple drawing primitives (points, lines, rectangles, ellipses, etc.)
* Supports text drawing and font settings
* Allows customization of drawing colors and styles
* * *
## Basic Usage of ImageDraw
### Creating a Draw Object
Before using ImageDraw, you need to create an `ImageDraw.Draw` object, passing an `Image` object as the base for drawing.
## Example
from PIL import Image, ImageDraw
# Create a blank image (white background)
image = Image.new("RGB",(400,300),"white")
# Create Draw object
draw = ImageDraw.Draw(image)
### Drawing Basic Shapes
ImageDraw supports various drawing methods, such as:
* **Draw lines**: `line()`
* **Draw rectangles**: `rectangle()`
* **Draw circles/ellipses**: `ellipse()`
* **Draw polygons**: `polygon()`
* **Draw text**: `text()`
* * *
## Common Methods of ImageDraw
| Method | Description | Example |
| --- | --- | --- |
| `line(xy, fill=None, width=1)` | Draws a line, `xy` is a coordinate tuple ((x1, y1, x2, y2)) | `draw.line([(10, 10), (100, 50)], fill="red", width=2)` |
| `rectangle(xy, fill=None, outline=None, width=1)` | Draws a rectangle, `xy` are the top-left and bottom-right coordinates | `draw.rectangle([(50, 50), (150, 150)], fill="blue", outline="black")` |
| `ellipse(xy, fill=None, outline=None, width=1)` | Draws an ellipse or circle | `draw.ellipse([(100, 100), (200, 200)], fill="green")` |
| `polygon(xy, fill=None, outline=None)` | Draws a polygon | `draw.polygon([(50, 50), (100, 20), (150, 50)], fill="yellow")` |
| `text(xy, text, fill=None, font=None)` | Draws text on the image | `draw.text((50, 50), "Hello Pillow!", fill="black")` |
| `arc(xy, start, end, fill=None, width=1)` | Draws an arc | `draw.arc([(50, 50), (150, 150)], 0, 90, fill="red")` |
| `chord(xy, start, end, fill=None, outline=None)` | Draws a chord (arc + connecting line) | `draw.chord([(50, 50), (150, 150)], 0, 180, fill="blue")` |
| `pieslice(xy, start, end, fill=None, outline=None)` | Draws a pie slice | `draw.pieslice([(50, 50), (150, 150)], 0, 90, fill="green")` |
| `textbbox()` | Used to calculate the bounding box occupied by given text when drawn on the image | `draw.textbbox((50, 50), "Hello Pillow!", font=font)` |
* * *
## Comprehensive Example
The following is a complete example demonstrating how to use ImageDraw to draw various shapes and text:
## Example
from PIL import Image, ImageDraw, ImageFont
# Create a blank image
image = Image.new("RGB",(500,400),"white")
draw = ImageDraw.Draw(image)
# Draw a line
draw.line([(10,10),(490,10)], fill="red", width=3)
# Draw a rectangle
draw.rectangle([(50,50),(150,150)], fill="blue", outline="black")
# Draw a circle
draw.ellipse([(200,50),(300,150)], fill="green")
# Draw a polygon
draw.polygon([(350,50),(400,100),(450,50),(400,150)], fill="yellow")
# Draw text
font = ImageFont.load_default()
draw.text((50,200),"Hello Pillow!", fill="black", font=font)
# Save the image
image.save("draw_example.png")
After running, an image file `draw_example.png` containing various shapes will be generated.
* * *
## Summary
ImageDraw is a very practical drawing module in Pillow, suitable for:
* Image annotation (e.g., adding text, arrows)
* Generating simple charts or diagrams
* Custom graphic drawing
By mastering methods like `line()`, `rectangle()`, `ellipse()`, and `text()`, you can easily achieve various drawing requirements.
If you need more advanced drawing functionalities, you can further extend the capabilities by combining `ImageFont` (font settings) or `ImageFilter` (filter effects).
Hope this tutorial helps you quickly get started with the ImageDraw module of Pillow!