''' Purpose: introduce Image and ImageDraw class modules ''' # should we show intermediary graphics SHOWING_INTERMEDIARY = True # set to True if we want to see each step of the drawing one by one # set to False if we only want to see the finished image # import necessary Pillow components from PIL import Image, ImageDraw # set the dimensions of the new Image im_width = 480 im_height = 400 dimensions = ( im_width, im_height ) # create a new Image with a black background im = Image.new( 'RGB', dimensions, color='Black' ) #must follow this format to make a new image # color = 'Black' --> black color background (can be any color) # get a drawing surface on the image canvas = ImageDraw.Draw( im ) # this allows us to draw on the image --> hand back surface of image if ( SHOWING_INTERMEDIARY == True ) : im.show() # DRAW RECTANGLE # shape always starts at the upper left corner and ends at the lower right hand corner # specify the coordinate for the upper left corner and the coordinates for the lower right corner # add a rectangle to the canvas x = 40 # x of the upper left corner y = 80 # y of the upper left corner w = 50 # width of rectangle h = 75 # height of rectangle xy = [ ( x, y ), ( x + w, y + h ) ] # put the coordinates in a list # coordinates are put into tuples, which are values within () # here, we use the function rectangle() from the canvas to draw the rectangle # rectangle() needs the dimensions of the rectangle as the first argument (xy = list of coordinates) # outline = set the color of the outline of the rectangle canvas.rectangle( xy, outline='Cornsilk' ) # we will get a rectangle starting at coordinate x = 40, y = 80 and its outline will have the color # Cornsilk if ( SHOWING_INTERMEDIARY == True ) : im.show() # DRAW A RECTANGLE THAT'S FILLED IN # add a filled-in rectangle to the canvas x = 100 # x coordinate of the upper left corner y = 20 # y coordinate of the upper left corner w = 100 # width of the rectangle h = 25 # height of the rectangle xy = [ (x, y), (x + w, y + h) ] # list of coordinate containing the coordinate for the upper left corner # and the coordinate of the lower right corner canvas.rectangle( xy, fill='Lavender', outline='Orchid' ) # fill = 'SomeColor' --> set the color to fill in the rectangle # can also draw a square if w == h if ( SHOWING_INTERMEDIARY == True ) : im.show() # DRAW ELLIPSE # draw circle if w == h # add an ellipse to the canvas x = 120 y = 65 w = 150 h = 90 xy = [ (x, y), (x + w, y + h) ] canvas.ellipse( xy, outline='Magenta' ) if ( SHOWING_INTERMEDIARY == True ) : im.show() # DRAW A FILLED IN ELLIPSE # add a filled-in ellipse to the canvas x = 300 # x coordinate of the upper left corner of the BOX THAT SURROUNDS THE ELLIPSE y = 25 # y coordinate of the upper left corner of the BOX THAT SURROUNDS THE ELLIPSE w = 50 # width of the box around the ellipse h = 75 # height of the box around the ellipse xy = [ (x, y), (x + w, y + h) ] canvas.ellipse( xy, fill='DeepPink', outline='GhostWhite' ) canvas.rectangle( xy, outline="Yellow" ) # draws the rctangular outline around the ellipse if ( SHOWING_INTERMEDIARY == True ) : im.show() # ADD SOME TEXT TO THE CANVAS # add text to the canvas coord = ( 25, 200 ) # the coordinate of the upper left corner of the rectangle to write the text in s = 'We are the best' # what is the text? # canvas.text() --> put the text on the canvas # must give it the coordinate first # then the text string # then fill = 'SomeColor' --> the color of the text # can do other things to text like change font, size, etc... canvas.text( coord, s, fill='Moccasin' ) if ( SHOWING_INTERMEDIARY == True ) : im.show() # DRAW A LINE # add a line to the canvas p0 = ( 25, 215 ) # coordinate where the line starts p1 = ( 115, 215 ) # coordinate where the line stops xy = [ p0, p1 ] # canvas.line() --> draw the line on canvas # get the coordinates of the start and stop as a list in xy # then fill in the color canvas.line( xy, fill='Moccasin' ) if ( SHOWING_INTERMEDIARY == True ) : im.show() # DRAW POLYGON # add a polygon to the canvas # get all the points on the polygon --> will draw a line between the points in the order that you specify 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 ] # the order of the coordinate matters! Draw a line between these points in order # connect the line point to the first point to close the polygon # canvas.polygon() --> draw the polygon # give it a list of coordinates canvas.polygon( seq, outline='Peru' ) if ( SHOWING_INTERMEDIARY == True ) : im.show() # 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 ] canvas.polygon( seq, fill='RebeccaPurple' ) if ( SHOWING_INTERMEDIARY == True ) : im.show() ''' # add an arc to the canvas p0 = ( 140, 220 ) p1 = ( 340, 380 ) xy = [ p0, p1 ] a1 = 0 a2 = 90 canvas.arc( xy, a1, a2, fill='RoyalBlue' ) if ( SHOWING_INTERMEDIARY == True ) : im.show() # add a chord to the canvas p0 = ( 140, 220 ) p1 = ( 340, 380 ) xy = [ p0, p1 ] a1 = 150 a2 = 210 canvas.chord( xy, a1, a2, fill='MediumTurquoise' ) if ( SHOWING_INTERMEDIARY == True ) : im.show() # add a pie slice to the canvas 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() '''