""" Purpose: Show off a few drawing commands Author: Luther Tychonievich (lat7h) License: Released to the public domain """ from PIL import Image from PIL import ImageDraw width = 400 # pixels height = 300 # pixels dimensions = (width, height) # An image stores pixels, but does not know how to draw anything else image = Image.new('RGB', dimensions, color='yellow') # So we make something that can draw on that image, here called "canvas" canvas = ImageDraw.Draw(image) # A line, between any two points we want. # Optionally, can give color and/or width # Remember: (0,0) is the top-left corner; y goes down, not up canvas.line( [ (10, 20), (50, 100) ], fill=(0,0,0), width=4 ) # A block of color, given by two opposite corners # Optionally, can give fill and/or outline colors canvas.rectangle( [ (110, 20), (150, 100) ], fill=(255,0,0), outline=(0,0,255), width=2 ) # An ellipse (or circle), given by two opposite corners of its bounding rectangle canvas.ellipse( [ (120, 30), (140, 90) ], fill=(0,127,0), outline=(0,0,0), width=2 ) # An arc: part of an ellipse canvas.arc( [ (10, 130), (50, 200) ], # rectangle it fits in 0, 270, # starting and stoping angles, in degrees fill=(0,0,0), width=2 ) # An chord: part of an ellipse, closed and optionally filled in canvas.chord( [ (110, 130), (150, 200) ], # rectangle it fits in 0, 270, # starting and stoping angles, in degrees fill=(255,255,255), outline=(0, 127, 255), width=4 ) # An pie slice: part of an ellipse, closed and optionally filled in canvas.pieslice( [ (210, 130), (250, 200) ], # rectangle it fits in 0, 270, # starting and stoping angles, in degrees fill=(255,255,255), outline=(0, 127, 255), width=4 ) # A polygon: a list of points, connected in order and closed canvas.polygon( [ (300, 0), (350, 10), (400, 50), (300, 200), (380, 300) ], fill=(255,0,0), outline=(0,0,255) ) # Text; unfortunately, its handling of fonts, etc, is computer-specific # so I can't give examples of that which will work on all students' machines canvas.text( (30, 270), "This is a display of example components", fill=(255,127,0), ) image.show() # pops up a window image.save('drawing_components.jpg') # creates (or overwrites) a file