''' Purpose: introduce Image and ImageDraw class modules ''' # This file will be very useful for artistry.py! # In artistry.py (an Imaging homework assignment), you will be using Image and # ImageDraw (a Pillow module that allows you to create images / art # kind of like Microsoft Paint with Python) # should we show intermediary graphics SHOWING_INTERMEDIARY = True # 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' ) # Image.new( mode, dimensions, background color ) -> this function takes in a color mode, # (in this case we're using the RGB palette which means we specify the amount of red, green, and blue # of each pixel), dimensions (basically the width and height of your image the same way any other image # is a rectangle / square), and the background color that we can draw over. # get a drawing surface on the image canvas = ImageDraw.Draw( im ) # Create a canvas out of our blank canvas (which is a black rectangle defined in im # on line 22) # if ( SHOWING_INTERMEDIARY == True ) : # im.show() # add a rectangle to the canvas x = 40 y = 80 w = 50 h = 75 xy = [ ( x, y ), ( x + w, y + h ) ] # ^ upper left hand corner of box (x,y) and bottom right hand corner is (x+w, y+h) # Starting position of rectangle (like you dragged it down and right for the rest of the rectangle # One of the functions you can use # from ImageDraw is rectangle! # rectangle draws a rectangle with the # specifications defined above ^ # You give it the rectangle location / dimensions # xy and an optional parameter for the # outline color. canvas.rectangle( xy, outline='Cornsilk' ) # if ( SHOWING_INTERMEDIARY == True ) : # im.show() # add a filled-in rectangle to the canvas x = 100 y = 20 w = 100 h = 25 xy = [ (x, y), (x + w, y + h) ] canvas.rectangle( xy, fill='Lavender', outline='Orchid' ) # You can also specify an optional parameter called fill ^ # Outline is the border color of the rectangle and the fill is what color the # inside of the rectangle will be! # if ( SHOWING_INTERMEDIARY == True ) : # im.show() # 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' ) # canvas.ellipse() is another ImageDraw function for an oval (you can use it for a circle too) # An oval works the same way as rectangle where you specify starting point # and the width and height of the oval # if ( SHOWING_INTERMEDIARY == True ) : # im.show() # add a filled-in ellipse to the canvas x = 300 y = 25 w = 50 h = 75 xy = [ (x, y), (x + w, y + h) ] canvas.ellipse( xy, fill='DeepPink', outline='GhostWhite' ) # if ( SHOWING_INTERMEDIARY == True ) : # im.show() # add text to the canvas coord = ( 25, 200 ) s = 'We are the best' canvas.text( coord, s, fill='Moccasin' ) # To add text to the image, use canvas.text() # Specify the location in the image you want to put that text (coord) # And the second optional parameter is the color of your text # if ( SHOWING_INTERMEDIARY == True ) : # im.show() # add a line to the canvas p0 = ( 25, 215 ) p1 = ( 115, 215 ) xy = [ p0, p1 ] canvas.line( xy, fill='Moccasin' ) # canvas.line( xy is a list with the starting point and ending point, optional fill is the color of your line) # if ( SHOWING_INTERMEDIARY == True ) : # im.show() # 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 ] canvas.polygon( seq, outline='Peru' ) # canvas.polygon( list of points for your polygon, outline is going to be the border color of your polygon) # canvas.polygon will connect all of these points with lines sequentially # This means it connects p0 to p1, then p1 to p2, then p2 to p3, and so on... # if ( SHOWING_INTERMEDIARY == True ) : # im.show() # add a filled-in polygon to the canvas p0 = (175, 80) p1 = (240, 110) p2 = (190, 230) p3 = (150, 120) seq = [ p0, p1, p2, p3 ] canvas.polygon( seq, fill='RebeccaPurple' ) # You can also specify a fill color for a polygon! # Again you give it a list of points to connect (seq) and then the fill color! # ImageDraw: ORDER MATTERS # The order you draw things matters in that your most recent code will be put in front # of things defined before in the code if it overlaps! # Like how the polygon will overlap and be placed in front of the ellipse. # 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' ) # canvas.arc( xy is a list of the starting point and ending point of our arc, # a1 and a2 are going to be our angles in between the two points, # and the fill is just the color of the arc itself) # 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' ) # A chord is basically like an arc but it's filled in (it's like a pie slice but smaller / thinner filled in arc) # In this one, we also specify points for xy (two points we want to connect), # the angles between them a1 to a2, and the fill of the chord. # 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' ) # canvas.pieslice( xy takes in two points, a1 and a2 are the angles between, and the fill is also specified as # the color of the pieslice ) - pieslice is like part of a circle from one angle to another # show the final image im.show()