Home Schedule
Examples of copying and transforming an image. Download Complete Example

import media

# Copy an image to a new blank canvas
def copy_image(original_image, start_x, start_y): 
    # Create a new cavas of a given size 
    new_canvas = media.makeEmptyPicture(800,500) 
    
    # Destination x of the canvas
    dest_x = start_x 
    
    # Loop over the image x coordinates
    for source_x in range(media.getWidth(original_image)): 
        # Destination y of the canvas
        dest_y = start_y
        
        # Loop over the image y coordinates        
        for source_y in range(media.getHeight(original_image)):
            source_color = media.getColor(media.getPixel(original_image, source_x, source_y))
            media.setColor(media.getPixel(new_canvas, dest_x, dest_y), source_color) 
            
            # Update the position of the y in the canvas 
            dest_y = dest_y + 1 
        # Update the position of the x in the canvas        
        dest_x = dest_x + 1
    
    return new_canvas 


# Isolate a region of the image to be copied to a new canvas 
def crop_image(original_image, tl_x, tl_y, br_x, br_y): 
    # Calculate a new width and height of the cropped region 
    new_canvas = media.makeEmptyPicture(br_x - tl_x, br_y - tl_y) 
    
    # Destination x of the canvas 
    dest_x = 0
    
    # Loop over the x values in the region 
    for source_x in range(tl_x, br_x): 
        # Destination y of the canvas 
        dest_y = 0 

        # Loop over the y values in the region        
        for source_y in range(tl_y, br_y): 
            source_color = media.getColor(media.getPixel(original_image, source_x, source_y))
            media.setColor(media.getPixel(new_canvas, dest_x, dest_y), source_color)      
            # Update the position of the y in the canvas 
            dest_y = dest_y + 1  
        # Update the position of the x in the canvas        
        dest_x = dest_x + 1
    
    return new_canvas 

# Flip in image over the verticle axis 
def flip_vertical(original_image): 
    # Get the width and height of the image
    width = media.getWidth(original_image)
    height = media.getHeight(original_image) 
    
    # Create an empty canvas that's the same size of the
    #   original image 
    new_canvas = media.makeEmptyPicture(width, height) 
    
    # Start copying pixels to the right most
    #   x values of the canvas 
    dest_x = width - 1 
    
    
    # Loop over the x values of the source image 
    for source_x in range(width): 
        # Start copying pixels to the top most
        #   y values of the canvas 
        dest_y = 0 
        
        # Loop over the y values in source image    
        for source_y in range(height):
            source_color = media.getColor(media.getPixel(original_image, source_x, source_y))
            media.setColor(media.getPixel(new_canvas, dest_x, dest_y), source_color) 
            
            # Update the position of the y in the canvas 
            dest_y = dest_y + 1 
        # Update the position of the x in the canvas        
        dest_x = dest_x - 1
            
    return new_canvas 

# Flip in image over the horizontal axis 
def flip_horizontal(original_image): 
    # Get the width and height of the image 
    width = media.getWidth(original_image)
    height = media.getHeight(original_image) 
    
    # Create an empty canvas that's the same size of the
    #   original image 
    new_canvas = media.makeEmptyPicture(width, height) 
    
    # Start copying pixels to the left most
    #   x values of the canvas 
    dest_x = 0 
    
    
    # Loop over the x values of the source image 
    for source_x in range(width): 
        # Start copying pixels to the bottom most
        #   y values of the canvas 
        dest_y = height - 1 
        
        # Loop over the y values in source image 
        for source_y in range(height):
            source_color = media.getColor(media.getPixel(original_image, source_x, source_y))
            media.setColor(media.getPixel(new_canvas, dest_x, dest_y), source_color) 
            
            # Update the position of the y in the canvas 
            dest_y = dest_y - 1 
        # Update the position of the x in the canvas  
        dest_x = dest_x + 1
            
    return new_canvas 

# Rotate an image clockwise 90 degrees 
def rotate_clockwise(original_image): 
    # Get the width and height of the image 
    width = media.getWidth(original_image)
    height = media.getHeight(original_image) 
    
    # Create an empty canvas that's the same size of the
    #   original image, but the dimensions are swapped 
    new_canvas = media.makeEmptyPicture(height, width) 
    
    # Treat the destination y values in the canvas
    #   like the width 
    dest_y = 0 
    
    # Loop over the x values of the source image 
    for source_x in range(width):
        dest_x = height - 1 
        
        # Loop over the y values in source image 
        for source_y in range(height):
            source_color = media.getColor(media.getPixel(original_image, source_x, source_y))
            media.setColor(media.getPixel(new_canvas, dest_x, dest_y), source_color) 
            
            # Update the position of the x in the canvas 
            dest_x = dest_x - 1 
        # Update the position of the y in the canvas 
        dest_y = dest_y + 1
            
    return new_canvas 

# Scale and image down (reduce the dimensions of the image) 
#   this function halves the images dimensions 
def scale_down(original_image):
    # Get the width and height of the image
    width = media.getWidth(original_image)
    height = media.getHeight(original_image) 
    
    # Create an empty canvas that's half the size of the
    #   original image 
    new_canvas = media.makeEmptyPicture(width//2, height//2) 
    
    # Original image source x 
    source_x = 0 
    
    # Loop over the canvas destination x values 
    for dest_x in range(0, width//2): 
        # Original image source y 
        source_y = 0 
        
        # Loop over the canvas destination x values 
        for dest_y in range(0, height//2):
            source_color = media.getColor(media.getPixel(original_image, source_x, source_y))
            media.setColor(media.getPixel(new_canvas, dest_x, dest_y), source_color) 
            
            # Take every other y pixel 
            source_y = source_y + 2 
        # Take every other x pixel 
        source_x = source_x + 2
            
    return new_canvas 

# Scale up algorithm based on the working version in the book
#    only doubles an image 
def scale_up(original_image): 
    # Get the width and height of the image 
    width = media.getWidth(original_image)
    height = media.getHeight(original_image) 
    
    # Get the width and height of the image multiplied by two 
    new_canvas = media.makeEmptyPicture(width*2, height*2) 
    
    # Source x of the source image 
    source_x = 0 
    
    # Loop over the x values of the canvas 
    for dest_x in range(0, width*2): 
        # Source y of the source image 
        source_y = 0 
        
        # Loop over the y values of the canvas 
        for dest_y in range(0, height*2): 
            # Convert the floating point source x and y to an int
            #   because we cannot access pixels at non integer positions
            #   This is also a part of the magic 
            source_color = media.getColor(media.getPixel(original_image, int(source_x), int(source_y)))
            media.setColor(media.getPixel(new_canvas, dest_x, dest_y), source_color) 
            
            # The adding by 0.5 is the magic
            #   This allows us to copy the correct number of source pixels
            #   on to our canvas 
            source_y = source_y + 0.5
            
        source_x = source_x + 0.5
            
    return new_canvas 


# A function to scale up an image based on an integer factor
#    factor is an integer from >= 1 
def scale_up_general(original_image, factor):
    # Get the width and height of the image
    width = media.getWidth(original_image)
    height = media.getHeight(original_image) 
    
    # Get the width and height of the image multiplied by the factor 
    new_canvas = media.makeEmptyPicture(width*factor, height*factor) 
    
    # Destination x of the canvas 
    dest_x = 0 
    
    # Loop over the x values of the source image  
    for source_x in range(width): 
        # Destination y of the canvas 
        dest_y = 0 
        
        # Loop over the y values of the source image 
        for source_y in range(height):
            source_pixel = media.getPixel(original_image, source_x, source_y)
            source_color = media.getColor(source_pixel) 
            
            # The loop makes sure that we copy the source pixel to the correct
            #   destination pixels in our canvas. This only runs based on
            #   the factor we used to increase the size of the image. 
            for sub_x in range(factor):
                for sub_y in range(factor):
                    dest_pixel = media.getPixel(new_canvas,  dest_x + sub_x, dest_y + sub_y)
                    media.setColor(dest_pixel, source_color) 
            
            # Move to the next group of pixels in the canvas based on the factor 
            dest_y += factor
        dest_x += factor
    
    return new_canvas 


# A function to scale up an image based on an integer factor
#    factor is an integer from >= 1
#    This function allows for you to declare a specific x/y region to
#    scale up 
def scale_up_region(original_image, start_x, start_y, end_x, end_y, factor):
    width = media.getWidth(original_image)
    height = media.getHeight(original_image) 
    
    # Make the new canvas based on the size of the region multiplied by the
    #    scale up factor 
    new_canvas = media.makeEmptyPicture((end_x - start_x) * factor, (end_y - start_y) * factor)
    
    dest_x = 0
    for source_x in range(start_x, end_x):
        dest_y = 0
        for source_y in range(start_y, end_y):
            source_pixel = media.getPixel(original_image, source_x, source_y)
            source_color = media.getColor(source_pixel)
            
            for sub_x in range(factor):
                for sub_y in range(factor):
                    dest_pixel = media.getPixel(new_canvas,  dest_x + sub_x, dest_y + sub_y)
                    media.setColor(dest_pixel, source_color)
            
            dest_y += factor
            
        dest_x += factor
    
    return new_canvas
            

lizard = media.makePicture('images/lizard.jpg')
ostrich = media.makePicture('images/ostrich.jpg')

media.show(lizard) 
# Copy image to a fixed size canvas 
media.show(copy_image(lizard, 50, 50)) 

# Crop a specific region of the image to
#   a new canvas 
media.show(lizard)
media.show(crop_image(lizard, 252, 82, 364, 169)) 

# Use the ostric image to show flips and
#   rotations 
media.show(ostrich)
media.show(flip_vertical(ostrich))
media.show(ostrich)
media.show(flip_horizontal(ostrich))
media.show(ostrich)
media.show(rotate_clockwise(ostrich)) 

# Shink the lizard image by half 
media.show(lizard)
media.show(scale_down(lizard)) 

# Scale up the lizard using the book's function 
media.show(lizard)
media.show(scale_up(lizard))

media.show(lizard) 
# Scale up the lizard image 3 times it's size
#   the image doesn't display correctly with show 
media.writePictureTo(scale_up_general(lizard, 3), 'output.jpg')

media.show(lizard) 
# Scale up just the lizards face 
media.show(scale_up_region(lizard, 252, 82, 364, 169, 2))

media.quit()