Pillow is a Python imaging library and a maintained fork of the discontinued Python Imaging Library (PIL) that adds image processing capabilities to Python and provides multiple modules for working with and modifying images.
In this article, we explore the ImageDraw module of Pillow, which provides methods to draw shapes, lines, and formatted text directly on images.
To download the image used in this article, click here.
Opening and Displaying the Image
First, let us load and display an image using Pillow:
from PIL import Image, ImageDraw
img = Image.open("gfg.png")
img.show()
Output

Note: The image must be in the same folder as the Python script otherwise, provide a relative or full path.
Creating a Draw Object
To perform any drawing operation, we must create a Draw object associated with the image:
draw = ImageDraw.Draw(img)
All drawing operations are performed using this object.
Drawing a Rectangle Â
To draw a rectangle, use the rectangle() method. Below is the syntax to draw rectangle in python pillow.
ImageDraw.rectangle(xy, fill, outline, width)
Parameters
- xy: Tuple (x1, y1, x2, y2) representing the top-left and bottom-right corners.
- fill: RGB tuple to fill the rectangle.
- outline: RGB tuple for the border color.
- width: Thickness of the border.
from PIL import Image, ImageDraw
img = Image.open("gfg.png")
draw = ImageDraw.Draw(img)
draw.rectangle(
xy=(50, 50, 150, 150),
fill=(0, 127, 0),
outline=(255, 255, 255),
width=5
)
img.show()
Output

Drawing an Ellipse(Circle)
To draw an ellipse or circle, use the ellipse() method. Below is the syntax to draw Ellipse in python pillow.
ImageDraw.ellipse(xy, fill=None, outline=None, width=1)
The co-ordinates you will provide in 'xy' will act as a box in which the circle will be enclosed.
from PIL import Image, ImageDraw
img = Image.open("gfg.png")
draw = ImageDraw.Draw(img)
draw.ellipse(
xy=(50, 50, 150, 150),
fill=(0, 127, 0),
outline=(255, 255, 255),
width=5
)
img.show()
Output

Drawing a Line
To draw a straight line, use the line() method. Below is the syntax to draw Line in python pillow.
ImageDraw.line(xy, fill=None, width=1)
Here, the outline parameter is not considered, and the width will determine how long the line should be.
from PIL import Image, ImageDraw
img = Image.open("gfg.png")
draw = ImageDraw.Draw(img)
draw.line(
xy=(50, 150, 150, 50),
fill=(0, 128, 0),
width=5
)
img.show()
Output

Drawing a Polygon
To draw a polygon, use the polygon() method. Below is the syntax to draw Polygon in python pillow.
ImageDraw.polygon(xy, fill=None, outline=None)
The xy parameter will contain coordinates based on the number of sides you want for your shape. Here, the width parameter is not valid.
from PIL import Image, ImageDraw
img = Image.open("gfg.png")
draw = ImageDraw.Draw(img)
draw.polygon(
xy=[(100, 50), (150, 100), (100, 150), (50, 100)],
fill=(0, 128, 0),
outline=(255, 255, 255)
)
img.show()
Output

Other Supported Shapes
The ImageDraw module also supports the following shapes:
- Arc: ImageDraw.arc(xy, start, end, fill, width)
- Chord: ImageDraw.chord(xy, start, end, fill, outline, width)
- Point: ImageDraw.point(xy, fill)
- Pieslice: ImageDraw.pieslice(xy, start, end, fill, outline, width)
The start and end parameters correspond to the degree of angles in clockwise direction, which will be connected with a line.
Writing Text on an Image
The ImageDraw module can also be used to write text on images using the text() method. Below is the syntax to write text on an image.
ImageDraw.text(xy, text, fill=None, font=None)
from PIL import Image, ImageDraw, ImageFont
img = Image.open("gfg.png")
fnt = ImageFont.truetype("Pillow/Tests/fonts/FreeMono.ttf", 20)
draw = ImageDraw.Draw(img)
draw.text(xy=(25, 160),
text="Hello, Geeks!",
font=fnt,
fill=(0, 127, 0))
img.show()
Output

Note: Ensure that the font path points to a valid .ttf file available on your system.