Python Pillow ImageDraw module

Python Pillow ImageDraw Module

ImageDraw provides simple 2D graphics support for Image objects. Generally, we use this module to create new images, annotate or modify existing images, and generate graphics on the fly for web use.

Graphics commands support drawing shapes and annotating text.

  • An image can be best understood as a two-dimensional array of pixels (picture elements). A pixel is the smallest supported color point.
  • The origin of the two-dimensional coordinate system used by ImageDraw is at the upper-left corner of the image.

  • The Pillow color scheme we use is RGB. RGB color representation and support is provided by the ImageColor module.

  • Bitmap, OpenType, or TrueType fonts are acceptable for text annotations.

  • Most drawing commands may require a bounding box parameter to specify the area on the image to which the command is applied.

  • A coordinate sequence can be represented as [(x0, y0), (x1, y1), … (xn, yn)].

  • For some drawing commands, angle values are required.

Example

The following Python example draws a line on a given image –

#Import required libraries
import sys
from PIL import Image, ImageDraw

#Create Image object
im = Image.open("images/logo.jpg")

#Draw line
draw = ImageDraw.Draw(im)
draw.line((0, 0) + im.size, fill=128)
draw.line((0, im.size[1], im.size[0], 0), fill=128)

#Show image
im.show()

Output

If you save the above program as Example.py and execute it, it will draw a line on the image and display it using standard PNG display tools as shown below.

Python Pillow - ImageDraw Module

Canvas

  • ImageDraw is a Pillow drawable surface (i.e., a canvas) for an image.
  • ImageDraw.Draw(img) returns a drawable canvas representation of the image parameter img. The canvas’s background is the image “img.”

Example

The following Python example draws text on a given image:

#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# Get an image
base = Image.open('images/boy.jpg').convert('RGBA')

# Make a blank image for the text, initialized to transparent text color
txt = Image.new('RGBA', base.size, (255,255,255,0))

# get a font
fnt = ImageFont.truetype('E:/PythonPillow/Fonts/Pacifico.ttf', 40)

# get a drawing context
d = ImageDraw.Draw(txt)

# draw text, half opacity
d.text((14,14), "Tutorials", font=fnt, fill=(255,255,255,128))

# draw text, full opacity
d.text((14,60), "Point", font=fnt, fill=(255,255,255,255))
out = Image.alpha_composite(base, txt)

#Showimage
out.show()

Output

Python Pillow - ImageDraw module

Drawing Shapes with the ImageDraw Module

The ImageDraw module allows you to create various shapes. First, create a drawing object with the image you want to work with, then apply it. We can use the ImageDraw module to draw some common shapes, as shown below.

Lines

The following is the syntax for drawing a line using Python Pillow:

draw.line(xy, fill=None, width=0)

line() draws a line from the bounding box xy and the top-left corner of the canvas to the bottom-right corner. The line is filled with the color. The fill and width parameters are optional and default to None and 0, respectively.

Examples

from PIL import Image, ImageDraw

img = Image.new('RGB', (500, 300), (125, 125, 125))
draw = ImageDraw.Draw(img)
draw.line((200, 100, 300, 200), fill=(0, 0, 0), width=10)

img.show()

Output

Python Pillow - ImageDraw module

Ellipse

The following is the syntax for drawing an ellipse using pillow:

draw.ellipse(xy, fill=None, outline=None)

ellipse() draws an ellipse bounded by the bounding box xy. The shape is filled with a color and outlined with a color. The default value is None; the fill and width parameters are optional.

Examples

from PIL import Image, ImageDraw

img = Image.new('RGB', (500, 300), (125, 125, 125))
draw = ImageDraw.Draw(img)

draw.ellipse((200, 125, 300, 200), fill=(255, 0, 0), outline=(0, 0, 0))
img.show()

Output

Python Pillow - ImageDraw Moduledraw.rectangle(xy, fill=None, outline=None)

The

rectangle() method draws a rectangle given a bounding box xy. The shape is filled with a color and outlined with a color. The default value is None; the fill and width parameters are optional.

from PIL import Image, ImageDraw

img = Image.new('RGB', (500, 300), (125, 125, 125))
draw = ImageDraw.Draw(img)

draw.rectangle(
   (200, 125, 300, 200),
   fill=(255, 0, 0),
   outline=(0, 0, 0))
img.show()

Output

Python Pillow - ImageDraw module

Polygon

The following is the syntax for drawing a rectangle using Python pillow:

draw.polygon(seq, fill=None, outline=None)

polygon() draws a polygon by connecting the coordinate sequence seq with a straight line. The first and last coordinates in seq are also connected by a straight line. The shape is filled with the fill color, and the perimeter is filled with the outline color. The fill and outline parameters are optional and default to None.

from PIL import Image, ImageDraw

img = Image.new('RGB', (500, 300), (125, 125, 125))
draw = ImageDraw.Draw(img)

draw.polygon(
   ((200, 200), (300, 100), (250, 50)),
   fill=(255, 0, 0),
   outline=(0, 0, 0))
img.show()

Output

Python Pillow - ImageDraw module

Leave a Reply

Your email address will not be published. Required fields are marked *