''' Purpose: demonstrate pillow drawing commands ''' PAUSING = True from PIL import Image, ImageDraw import url # specify dimensions and background WIDTH = 480 HEIGHT = 400 BACKGROUND_COLOR = "black" # creat image of appropriate dimension im = Image.new( 'RGB', (WIDTH, HEIGHT), BACKGROUND_COLOR ) # get a drawable canvas of image im canvas = ImageDraw.Draw( im ) # paint the entire surface with the background color xy = [ (0, 0), (WIDTH, HEIGHT) ] canvas.rectangle( xy, fill=BACKGROUND_COLOR ) if ( PAUSING ) : im.show(); input( 'Hit enter when ready' ) # draw green 50 X 75 rectangle perimeter at (40, 80) x = 40 y = 80 w = 50 h = 75 xy = [ (x, y), (x + w, y + h) ] o = 'green' canvas.rectangle( xy, outline=o ) if ( PAUSING ) : im.show(); input( 'Hit enter when ready' ) # draw filled lavender 100 X 25 rectangle at (100, 20) with grey outline x = 100 y = 20 w = 100 h = 25 xy = [ (x, y), (x + w, y + h) ] f = 'lavender' o = 'grey' canvas.rectangle( xy, fill=f, outline=o ) if ( PAUSING ) : im.show(); input( 'Hit enter when ready' ) # draw magenta 150 X 90 oval at (120, 65) x = 120 y = 65 w = 150 h = 90 xy = [ (x, y), (x + w, y + h) ] o = 'magenta' canvas.ellipse( xy, outline=o ) if ( PAUSING ) : im.show(); input( 'Hit enter when ready' ) # draw filled light sky blue 50 X 75 oval at (300, 25) with perimeter #98fb98' x = 300 y = 25 w = 50 h = 75 xy = [ (x, y), (x + w, y + h) ] f = 'LightSkyBlue' o = '#98fb98' canvas.ellipse( xy, fill=f, outline=o ) if ( PAUSING ) : im.show(); input( 'Hit enter when ready' ) # draw string "We are the best!" in cyan at (300, 155) x = 300 y = 155 coord = (x, y) s = "We are the best!" f = 'cyan' canvas.text( coord, s ) if ( PAUSING ) : im.show(); input( 'Hit enter when ready' ) # draw #fff5ee line from (300, 175) to (380, 175) x0 = 300 y0 = 175 x1 = 395 y1 = 175 xy = [ (x0, y0), (x1, y1) ] f = '#fff5ee' canvas.line( xy, fill=f ) if ( PAUSING ) : im.show(); input( 'Hit enter when ready' ) # draw yellow-sided polygon using the points p1 = (350, 20) p2 = (400, 60) p3 = (425, 100) p4 = (425, 160) p5 = (400, 125) p6 = (375, 90) o = 'yellow' seq = [ p1, p2, p3, p4, p5, p6 ] canvas.polygon( seq, outline=o ) if ( PAUSING ) : im.show(); input( 'Hit enter when ready' ) # draw filled pink polygon using points p1 = (175, 80) p2 = (240, 110) p3 = (190, 130) p4 = (150, 120) c = 'Salmon' f = 'pink' seq = [ p1, p2, p3, p4 ] canvas.polygon( seq, fill=f ) # draw salmon colored arc p1 = ( 140, 220 ) p2 = ( 340, 380 ) xy = ( p1, p2 ) a1 = 0 a2 = 72 f = 'Salmon' canvas.arc( xy, a1, a2, fill=f ) # draw spring green filled chord p1 = ( 140, 220 ) p2 = ( 340, 380 ) xy = ( p1, p2 ) a1 = a1 + 72 a2 = a2 + 72 f = 'SpringGreen' canvas.chord( xy, a1, a2, fill=f ) # draw light coral filled chord with lemon chiffon outline p1 = ( 140, 220 ) p2 = ( 340, 380 ) xy = ( p1, p2 ) a1 = a1 + 72 a2 = a2 + 72 f = 'LightCoral' o = 'LemonChiffon' canvas.chord( xy, a1, a2, fill=f, outline=o ) # draw misty rose filled pieslice p1 = ( 140, 220 ) p2 = ( 340, 380 ) xy = ( p1, p2 ) a1 = a1 + 72 a2 = a2 + 72 f = 'MistyRose' canvas.pieslice( xy, a1, a2, fill=f ) # draw plum-filled pieslice with green-yello outline p1 = ( 140, 220 ) p2 = ( 340, 380 ) xy = ( p1, p2 ) a1 = a1 + 72 a2 = a2 + 72 f = 'Plum' o = 'GreenYellow' canvas.pieslice( xy, a1, a2, fill=f, outline=o ) # show p1-p2 line in Navajo white p1 = ( 140, 220 ) p2 = ( 340, 380 ) xy = ( p1, p2 ) f = 'NavajoWhite' canvas.line( xy, fill=f ) # let's see what we got im.show() im.save( 'paint.png' )