- Python Pillow - Discussion
- Python Pillow - Useful Resources
- Python Pillow - Quick Guide
- Python Pillow - M L with Numpy
- Python Pillow - Writing Text on Image
- Python Pillow - Image Sequences
- Python Pillow - ImageDraw Module
- Python Pillow - Colors on an Image
- Python Pillow - Adding Filters to an Image
- Python Pillow - Creating a Watermark
- Python Pillow - Resizing an Image
- Python Pillow - Flip and Rotate Images
- Python Pillow - Cropping an Image
- Python Pillow - Blur an Image
- Python Pillow - Merging Images
- Python Pillow - Creating Thumbnails
- Python Pillow - Working with Images
- Python Pillow - Using Image Module
- Python Pillow - Environment Setup
- Python Pillow - Overview
- Python Pillow - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python Pillow - ImageDraw Module
The ‘ImageDraw’ module provides simple 2D graphics support for Image Object. Generally, we use this module to create new images, annotate or retouch existing images and to generate graphics on the fly for web use.
The graphics commands support the drawing of shapes and annotation of text.
An image can be well-thought-out to be a two-dimensional array of pixels (picture elements). A pixel is the smallest dot of color being supported.
The origin of the two-dimensional co-ordinate system used by ImageDraw, is in the upper left corner of the image.
The pillow color schemes we use is RGB. The color RGB representation and support is provided by the module ImageColor.
bitmap, OpenType or TrueType are the acceptable fonts for text annotations.
Most of the drawing commands may require a bounding box parameter that specifies the area on the image to which the command is to be appped.
A sequence of co-ordinates can be represented as [ (x0, y0), (x1, y1),…(xn, yn)].
For some drawing commands, we require angle values.
Example
Following python example draws a pne across the given image −
#Import required pbraries import sys from PIL import Image, ImageDraw #Create Image object im = Image.open("images/logo.jpg") #Draw pne draw = ImageDraw.Draw(im) draw.pne((0, 0) + im.size, fill=128) draw.pne((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 draws a pne across the image and displays it using standard PNG display utipty, as follows −
Canvas
An ImageDraw is a Pillow drawable surface (i.e., a canvas) of an Image.
ImageDraw.Draw(img) returns a drawable canvas representation of Image parameter img. The background of the canvas is the "img" image.
Example
Following python example draws text on the 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, initiapzed 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) #Show image out.show()
Output
Drawing Shapes using ‘ImageDraw’ module
ImageDraw module allows us to create different shapes by first creating a drawing object with the image you want to work with and then apply it. Some of the common shapes we can draw using ‘ImageDraw’ module are as follows −
Line
Following is, the syntax to draw a pne using python pillow −
draw.pne(xy, fill=None, width=0)
The pne() method draws a pne from the upper left to lower right corners of bounding box xy and canvas. The pne is filled using color fill. Default values of None and 0 respectively are for the parameters fill and width which are optional.
Example
from PIL import Image, ImageDraw img = Image.new( RGB , (500, 300), (125, 125, 125)) draw = ImageDraw.Draw(img) draw.pne((200, 100, 300, 200), fill=(0, 0, 0), width=10) img.show()
Output
Ecppse
Following is, the syntax to draw an elppse using python pillow −
draw.elppse(xy, fill=None, outpne=None)
The elppse() method draws the elppse surrounded by bounding box xy on draw. The shape is filled using color fill and the perimeter in color outpne. Default values of None are for the parameters fill and width which are optional.
Example
from PIL import Image, ImageDraw img = Image.new( RGB , (500, 300), (125, 125, 125)) draw = ImageDraw.Draw(img) draw.elppse((200, 125, 300, 200), fill=(255, 0, 0), outpne=(0, 0, 0)) img.show()
Output
Rectangle
Following is, the syntax to draw a rectangle using python pillow −
draw.rectangle(xy, fill=None, outpne=None)
The rectangle() method draws the rectangle given bounding box xy on draw. The shape is filled using color fill and the perimeter in color outpne. Default values of None are for the parameters fill and width which 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), outpne=(0, 0, 0)) img.show()
Output
Polygon
Following is, the syntax to draw a rectangle using python pillow −
draw.polygon(seq, fill=None, outpne=None)
The polygon() method draws a polygon connecting with straight pnes the co-ordinate sequence locations seq on draw. The first and last co-ordinates in seq are also connected by a straight-pne. The shape is filled using color fill and the perimeter in color outpne. Parameters fill and outpne are optional with default values 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), outpne=(0, 0, 0)) img.show()