Pillow ImageDraw Module
ImageDraw is a module in the Python image processing library Pillow (a fork of PIL), used to draw various shapes and text on images.
ImageDraw provides rich drawing capabilities, including lines, rectangles, circles, polygons, text, etc., suitable for image annotation, generating charts, or simple graphic design.
ImageDraw The main features of the module include:
- Support drawing on existing images
- Can create brand-new blank canvases
- Provide multiple drawing primitives (points, lines, rectangles, ellipses, etc.)
- Support text drawing and font settings
- Allow customizing drawing colors and styles
Basic Usage of ImageDraw
Create a Draw Object
Before using ImageDraw, you need to first create a ImageDraw.Draw object, passing in an Image object as the basis for drawing.
Example
from
PIL
import
Image
,
ImageDraw
Create a blank image (white background)
image
=
Image.
new
(
"RGB"
,
(
400
,
300
)
,
"white"
)
Create a Draw Object
draw
=
ImageDraw.
Draw
(
image
)
Draw Basic Shapes
ImageDraw supports multiple drawing methods, for example:
- Draw a line:
line() - Draw a rectangle:
rectangle() - Draw a circle/ellipse:
ellipse() - Draw a polygon:
polygon() - Draw the text:
text()
Common ImageDraw Methods
| Method | Description | Example |
|---|---|---|
line(xy, fill=None, width=1) |
Draw 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) |
Draw 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) |
Draw an ellipse or circle | draw.ellipse([(100, 100), (200, 200)], fill="green") |
polygon(xy, fill=None, outline=None) |
Draw a polygon | draw.polygon([(50, 50), (100, 20), (150, 50)], fill="yellow") |
text(xy, text, fill=None, font=None) |
Draw text on an image | draw.text((50, 50), "Hello Pillow!", fill="black") |
arc(xy, start, end, fill=None, width=1) |
Draw an arc | draw.arc([(50, 50), (150, 150)], 0, 90, fill="red") |
chord(xy, start, end, fill=None, outline=None) |
Draw a chord (arc + connecting line) | draw.chord([(50, 50), (150, 150)], 0, 180, fill="blue") |
pieslice(xy, start, end, fill=None, outline=None) |
Draw 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 an image | draw.textbbox((50, 50), "Hello Pillow!", font=font) |
Comprehensive Example
The following is a complete example showing 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 the 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 containing various shapes will be generated draw_example.png。
Summary
ImageDraw is a very practical drawing module in Pillow, suitable for:
- Image annotation (such as adding text, arrows)
- Generating simple charts or diagrams
- Custom graphic drawing
By mastering line()、rectangle()、ellipse()、text() and other methods, can easily achieve various drawing needs.
If you need more advanced drawing capabilities, you can combine with ImageFont(font settings) or ImageFilter(filter effects) to further extend the functionality.
Hope this tutorial helps you quickly get started with Pillow's ImageDraw module!