Python Pillow – ImageDraw Module
Python’s Pillow which is a fork of the discontinued Python Imaging Library (PIL) is a powerful library that is capable of adding image processing capabilities to your python code. Pillow offers many modules that ease the process of working and modifying images.
In this article, we will have a look at the ImageDraw module of this library. ImageDraw provides a variety of methods to, as its name suggests, draw on images. With the help of this module, we can draw lines, circles, rectangles and, even write and format text on an image.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Drawing common shapes on image
The image we will be using can be displayed using PIL as follows:
We can draw shapes and figures on an Image using the Draw method by firstly creating a Draw object.
Drawing a rectangle on the image:
For drawing a rectangle, we use the rectangle drawing method of the ImageDraw module:
Syntax: ImageDraw.rectangle(xy, fill, outline, width)
The paramaters for this method are:
- xy : Corresponds to the tuple of set of points in the upper left corner and lower right coordinates enclosing your shape. The points are passed in a tuple as follows : (upper left x-coordinate, upper left y-coordinate, lower right x-coordinate, lower right y-coordinate)
- fill : Corresponds to the tuple of RGB colour values to fill the shape with.
- outline : Corresponds to the tuple of RGB colour values assigned for the shape’s boundary.
- width : Integer value corresponding to the thickness of the boundary of the shape.NOTE: The parameters are similar across various shape drawing methods.
Drawing an ellipse(circle) on the image:
For drawing an ellipse shape, we use the ellipse method of the ImageDraw methods:
Syntax: ImageDraw.ellipse(xy, fill, outline, width)
The co-ordinates you will provide in xy will act as a box in which the circle will be enclosed.
Drawing a line on the image:
For drawing a line, we use the line method of the ImageDraw methods:
Syntax: lImageDraw.ine(xy, fill, width)
Here, the outline parameter is not considered and the width will determine how long the line should be.
Drawing a polygon on the image:
We can draw a polygon of the desired shape by using the polygon method of the ImageDraw methods:
Syntax: ImageDraw.polygon(xy, fill, outline)
The xy tuple parameter will contain co-ordinates based on the number of sides you want for your shape. Here, the width parameter is not valid.
Similarly, we can draw some other shapes using these methods:
- Arc: ImageDraw.arc(xy, start, end, fill, width)
- Chord (Bow-shape): 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 image:
Using our Draw object we can also write text on an image. It can be done using the Text method:
Syntax: ImageDraw.text(xy, text, fill, font, anchor, spacing, align, direction, features, language, stroke_width, stroke_fill, embedded_color)
We will also be using ImageFont from PIL to use the desired font for our text.