Pillow


from PIL import Image, ImageDraw


Image

rgb coloring

im_width = 480

im_height = 400

im = Image.new( 'RGB', ( im_width, im_height ), color='Black' )

pillow canvas

im.show()

im.save( 'canvas.jpg' )

Function save() expects a string parameter giving the name for the picture file. The file will be located in the same folder as the program that did the saving.


ImageDraw

canvas = ImageDraw.Draw( im )


x = 40

y = 80

w = 50

h = 75

xy = [ ( x, y ), ( x + w, y + h ) ]

canvas.rectangle( xy, outline='Cornsilk' )

Further, the shape is to be the outline of a rectangle with the outline colored green. Parameter outline is optional, if specified its value is to be the color of the perimeter.

perimenter rectangle

x = 100

y = 20

w = 100

h = 25

xy = [ (x, y), (x + w, y + h) ]

canvas.rectangle( xy, fill='Lavender', outline='Orchid' )

Adds a filled-in rectangle to our drawing.

filled rectangle

x = 120

y = 65

w = 150

h = 90

xy = [ (x, y), (x + w, y + h) ]

canvas.ellipse( xy, outline='Magenta' )

x = 300

y = 25

w = 50

h = 75

xy = [ (x, y), (x + w, y + h) ]

canvas.ellipse( xy, fill='DeepPink', outline='GhostWhite' )

Both ellipses have their perimeter drawn; and one also has its background coloorange red.

ellipses

coord = ( 25, 275 )

s = 'We are the best'

canvas.text( coord, s, fill='Moccasin' )

to add the string 'We are the best' to our drawing. The first parameter to the function is the location to start the text; the second parameter is the desired text.

text

p1 = ( 25, 290 )

p2 = ( 115, 290 )

xy = [ p1, p2 ]

canvas.text( xy, s, fill='Moccasin' )

adds a horizontal line below our text.

line

p1 = (350, 120)

p2 = (400, 140)

p3 = (425, 200)

p4 = (425, 260)

p5 = (375, 245)

p6 = (325, 190)

seq = [ p1, p2, p3, p4, p5, p6 ]

canvas.polygon( seq, outline='Peru' )

p1 = (175, 80)

p2 = (240, 110)

p3 = (190, 130)

p4 = (150, 120)

seq = [ p1, p2, p3, p4 ]

canvas.polygon( seq, fill='RebeccaPurple' )

arc

p1 = ( 140, 220 )

p2 = ( 340, 380 )

xy = ( p1, p2 )

a1 = 0

a2 = 90

canvas.arc( xy, a1, a2, fill='RoyalBlue' )

im.save( 'arc.jpg' )

Note, a 0° angle points east; a 90° angle points south; a 180° angle points west; a 270° angle points north.

arc

p1 = ( 140, 220 )

p2 = ( 340, 380 )

xy = [ p1, p2 ]

a1 = 150

a2 = 210

canvas.chord( xy, a1, a2, fill='MediumTurquoise' )

chord

p1 = ( 140, 220 )

p2 = ( 340, 380 )

xy = [ p1, p2 ]

a1 = 225

a2 = 315

canvas.pieslice( xy, a1, a2, fill='OrangeRed' )

pie slice