''' Purpose: support image resizing ''' # in_size and in_where basically take the original and make it smaller by # Making width and height 1/2 of the original # Making the spot 2x, def in_size( original ) : ''' Returns size of an image whose dimensions are half of original's dimensions ''' # Extract width and height from original image dimensions (original.size) ow , oh = original.size nw , nh = ow // 2, oh // 2 # Integer division because dimensions can't be # decimals! dimensions = ( nw,nh ) return dimensions def out_size( original ) : ''' Returns size of an image whose dimensions are twice of original's dimensions ''' # This is the opposite of zoom in! (Makes it bigger) ow, oh = original.size nw, nh = ow*2, oh*2 # We know now that the new image has twice the width and # twice the height dimensions = ( nw,nh ) return dimensions def in_where( nspot, original= None ) : ''' Returns where to look in an original whose dimensions are twice the size of the image where nspot is located. ''' # Function to determine the spot for the zoomed in version of the original # Extract the x and y from the spot passed in nx, ny = nspot # Extract nx and ny from npot ox, oy = 2*nx, 2*ny # The original is 2x the size of the new so we multiply # nx and ny by 2 ospot = (ox,oy) # Initialize ospot and set it to be that coordinate tuple return ospot # Return the modified spot ospot based on nspot passed in # The logic here is that we pass in nspot like "Hey we have a spot in the new # image and we want to figure out where to get that pixel from the original. # If we know that the original image is 2x larger, the spot we want to # get from the original is 2 times the x coordinate in the new image and 2 times # the y coordinate in new image. # We're saying "Pluck the spot for the new one from this spot in the old one." def out_where( nspot, original=None ) : ''' Returns where to look in an original whose dimensions are half the size of the image where nspot is located. ''' # Extract the nx and ny from nspot nx, ny = nspot ox, oy = nx // 2, ny // 2 ospot = ( ox,oy ) return ospot def color ( opixel ) : ''' Returns a new color equal to opixel ''' # Return a new color mean: # Extract the r,g,b # r,g,b = opixel (pixel passed in) # Use the r,g,b from the original pixel to return whatever modified # pixel you want r,g,b = opixel npixel = (r,g,b) return npixel def blur( original ) : ''' Returns a new image that is a 16 by 16 scaling of a 1/16 by 1/16 scaling of original ''' import transforms # This one returns a new IMAGE # 16 by 16 scaling of a 1/16 by 1/16 scaling of original # = make it smaller a bunch of times and then make it larger again # shrink it down to 1/16 its size and enlarge it to 16 times that shrinking # Shrink it 4 times and enlarge it 4 times! # What functions could you call? transforms.zoom_in and transforms.zoom_out! # Do this ^ im = original.copy() # We didn't want to change original so if you do im = original # then it work but you're changing the original too. # We just want a new version of the original image. # im = im.convert('RGB') for i in range(0,4): # Do this four times # Shrink the image im = transforms.zoom_in( im ) # returns a new image that is shrunken for i in range(0, 4): # Do this four times # Enlarge the image im = transforms.zoom_out(im) # returns a new image that is enlarged # You want to continuously assign im to the new modified version of im # called on the next stored version of im return im if ( __name__ == '__main__' ) : import url import transforms # get web image of interest # link = "http://www.cs.virginia.edu/~cs1112/images/mandrill/full.png" link = "full.jpg" # If you don't have access to the website and you # downloaded the image, you can just use the downloaded # image (make sure it's in the same folder as this file!) original = url.get_image( link ) # zoom_in( original ) shrinks it and zoom_out(original) enlarges it # Takes in a whole image and returns a whole image! # original.show() # im1 = transforms.zoom_in( original ) # im1.show() # # im2 = transforms.zoom_out( original ) # im2.show() im3 = blur( original ) im3.show()