''' Purpose: introduce Image and ImageDraw class modules ''' # should we show intermediary graphics SHOWING_INTERMEDIARY = False # import necessary Pillow components from PIL import Image, ImageDraw # set the dimensions of the new Image im_width = 480 # how many pixels wide should our canvas be im_height = 400 # how many pixels tall should our canvas be dimensions = ( im_width, im_height ) # create a new Image with a black background im = Image.new( 'RGB', dimensions, color='Black' ) # create a blank canvas, but we can't draw yet # get a drawing surface on the image canvas = ImageDraw.Draw( im ) # get drawing capabilities through ImageDraw # we have to do all of our drawing on the image stored in canvas if ( SHOWING_INTERMEDIARY == True ) : im.show() input( "enter when ready" ) # add a rectangle to the canvas x = 40 y = 80 w = 50 # since our x is 40, our bottom right corner will have x value of 90 ( x + w ) h = 75 # since our y is 80, our bottom right corner will have x value of 155 ( y + h ) # the positive y direction is defined as down xy = [ ( x, y ), ( x + w, y + h ) ] # specify 2 points: upper left hand corner (x, y) and bottom right corner (x+w, y+h) canvas.rectangle( xy, outline='Cornsilk' ) # can specify outline or fill, will default to original color as fill if ( SHOWING_INTERMEDIARY == True ) : im.show() input("enter when ready") # add a filled-in rectangle to the canvas x = 100 # over to the right 100 pixels y = 20 # 20 pixels down from the top w = 100 h = 25 xy = [ (x, y), (x + w, y + h) ] # [ upper left hand corner, bottom right hand corner ] , corners as ( ordered, pairs ) canvas.rectangle( xy, fill='Lavender', outline='Orchid' ) if ( SHOWING_INTERMEDIARY == True ) : im.show() input("enter when ready") # add an ellipse to the canvas x = 120 # symmetric ellipses are circles y = 65 w = 150 h = 90 xy = [ (x, y), (x + w, y + h) ] # ellipses can be enclosed in boxes, and are defined that way in Python # above, we define the boundaries of the box that will fit this oval canvas.ellipse( xy, outline='Magenta' ) if ( SHOWING_INTERMEDIARY == True ) : im.show() input("enter when ready") # add a filled-in ellipse to the canvas x = 300 # left corner of enclosing block is ( x, y ) y = 25 w = 50 # right corner of enclosing block is ( x + w, y + h ) h = 75 xy = [ (x, y), (x + w, y + h) ] # how to make a circle? set w = h canvas.ellipse( xy, fill='DeepPink', outline='GhostWhite' ) # order of fill and outline does not matter if ( SHOWING_INTERMEDIARY == True ) : im.show() input("enter when ready") # add text to the canvas coord = ( 25, 200 ) s = 'We are the best' canvas.text( coord, s, fill='Moccasin' ) # text command allows us to add words to the canvas # font problems? figure out how to change it. google "stack exchange pillow change font" if ( SHOWING_INTERMEDIARY == True ) : im.show() input("enter when ready") # add a line to the canvas p0 = ( 25, 215 ) # endpoint 1 <- these numbers come from trial and error often p1 = ( 115, 215 ) # endpoint 2 xy = [ p0, p1 ] # xy is your line segment canvas.line( xy, fill='Moccasin' ) if ( SHOWING_INTERMEDIARY == True ) : im.show() input("enter when ready") # add a polygon to the canvas p0 = (350, 120) p1 = (400, 140) p2 = (425, 200) p3 = (425, 260) p4 = (375, 245) p5 = (325, 190) seq = [ p0, p1, p2, p3, p4, p5 ] # this is a list of all 6 points defined above # when this is passed into the polygon() function, it will connect p0-p1-p2-p3-p4-p5-p0 canvas.polygon( seq, outline='Peru' ) if ( SHOWING_INTERMEDIARY == True ) : im.show() input("enter when ready") # add a filled-in polygon to the canvas p0 = (175, 80) p1 = (240, 110) p2 = (190, 130) p3 = (150, 120) seq = [ p0, p1, p2, p3 ] # you can make literally any polygon using this. it will just connect the points in order with straight line segments canvas.polygon( seq, fill='RebeccaPurple' ) if ( SHOWING_INTERMEDIARY == True ) : im.show() input("enter when ready") # add an arc to the canvas p0 = ( 140, 220 ) # define endpoints p0, p1 p1 = ( 340, 380 ) xy = [ p0, p1 ] # stick em together in a list - bounding box of arc # angles are measured down from the positive x axis a1 = 0 # angle - start at 0 a2 = 90 canvas.arc( xy, a1, a2, fill='RoyalBlue' ) if ( SHOWING_INTERMEDIARY == True ) : im.show() input("enter when ready") # add a chord to the canvas # chord is an arc with endpoints connected p0 = ( 140, 220 ) p1 = ( 340, 380 ) xy = [ p0, p1 ] # specify a list of points for bounding box a1 = 150 # define angles a2 = 210 canvas.chord( xy, a1, a2, fill='MediumTurquoise' ) # once something is on the image, it is not going to change. if ( SHOWING_INTERMEDIARY == True ) : im.show() input("enter when ready") # add a pie slice to the canvas # pie slice is arc with lines going to the center of the ellipse p0 = ( 140, 220 ) p1 = ( 340, 380 ) xy = [ p0, p1 ] a1 = 225 a2 = 315 canvas.pieslice( xy, a1, a2, fill='OrangeRed' ) # show the final image im.show()