Draw with Pillow

Pillow supports some basic drawing capabilities. To use them, we need to import the ImageDraw module:

from PIL import Image, ImageDraw

I will use a blank image:

img = Image.open('blank.jpg')

I need to create an ImageDraw object from our image:

idraw = ImageDraw.Draw(img)

I can now draw shapes on our image. For example, to draw a red rectangle, I need to use the rectangle() method and provide the coordinates (the upper right and lower left points) where the shape will be placed:

idraw.rectangle((179,15,254,282), fill='red')
img.show()

To draw a line, I can use the line() method and pass the x and y coordinates, along with the line color and width:

idraw.line((100,280, 330,280), fill='green', width=10)
img.show()

Geek University 2022