PIL Image Draw

Outside Resources

https://pillow.readthedocs.io/en/5.1.x/reference/ImageDraw.html


The PIL ImageDraw module allows you to draw shapes and text onto a picture.

The following example draws onto a blank canvas image. The image  is created using the new() function, but you can also draw on an existing image from the open() function.

The command “draw = PIL.ImageDraw.Draw(canvas)” creates an object called draw. Then the draw object can call a variety of functions to draw onto your image object.

Look through the example code and see how you can draw pixels, lines, polygons, arcs, and text.

The example code won’t work without a font file.
(download here: https://www.wfonts.com/font/dopestyle)

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import random

canvas = Image.new("RGB",(300,300),(255,255,100))

draw = ImageDraw.Draw(canvas)

for i in range(20000):
    x1 = random.randint(0,299)
    y1 = random.randint(0,299)
    r = random.randint(100,255)
    g = random.randint(100,255)
    b = random.randint(0,50)
    draw.point( (x1,y1), (r,g,b) )

draw.line( (299,0,0,299), fill = (60,5,90), width = 8)
draw.polygon( (230,240, 270,240, 280,290, 270, 299, 220,280), fill='green')
draw.ellipse( (5,5,45,45), fill=(60,5,90) )
draw.arc( (0,75,299,225), 60, 300, fill="black")

for x in range(0,255):        
    g = random.randint(50,100)
    draw.line((x+22,100,x+22,200),fill=(x,g,255-x))

for y in (5,105,185,245,275):
    draw.ellipse((130,y,170,y+20),fill=(255,100,100))

#load font; you also need to download a font file
dopestyle = ImageFont.truetype("dopestyle.ttf",40)

draw.text((55, 135), "I am Fancy ", font=dopestyle, fill = (200,200,255) )

canvas.show()