Home Schedule
Examples of copying and transforming an image. Download Complete Example
    
    
    import media 
    
    # Scale_up function from the previous class 
    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)
        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 
    
    # Blurring an image 
    def blur(original_image):
        target = media.duplicatePicture(original_image) 
        
        # Since we will be accessing all pixels that surround our pixel
        #   (top, left, right, and bottom) we use a range that starts at
        #   1 and ends at width - 1 and heigth - 1 to create a boundary
        #   around the top, bottom, left, and right most pixels to avoid
        #   going out the boundary of the image 
        for x in range(1, media.getWidth(original_image)-1):
            for y in range(1, media.getHeight(original_image)-1): 
                
                # Get the surrounding pixel info from the original
                #   image 
                top = media.getPixel(original_image, x, y-1)
                left = media.getPixel(original_image, x-1, y)
                right = media.getPixel(original_image, x+1, y)
                bottom = media.getPixel(original_image, x, y+1) 
                
                # We will take the center pixel from the duplicate
                # picture because we will change this pixel 
                center = media.getPixel(target, x, y) 
                
                # Average each of the color channels of the near by pixels to create
                #   new R, G, B values 
                newRed= (media.getRed(top) + media.getRed(left) + media.getRed(bottom) +
                         media.getRed(right) + media.getRed(center)) // 5
                
                newGreen= (media.getGreen(top) + media.getGreen(left) + media.getGreen(bottom) +
                           media.getGreen(right) + media.getGreen(center)) // 5
                
                newBlue= (media.getBlue(top) + media.getBlue(left) + media.getBlue(bottom) +
                          media.getBlue(right) + media.getBlue(center)) // 5 
                
                # Set the center pixel to be the "smoothed" color value 
                media.setColor(center, media.makeColor(newRed, newGreen, newBlue))
    
        return target 
    
    #Simple blending of two images 
    def basic_blend(pic1, pic2): 
        # Make a canvas based on the smallest image width and height 
        new_canvas = media.makeEmptyPicture(min(media.getWidth(pic1),media.getWidth(pic2)),
                                            min(media.getHeight(pic1),media.getHeight(pic2))) 
    
        
        # Get the width and height of the canvas 
        canvas_width = media.getWidth(new_canvas)
        canvas_height = media.getHeight(new_canvas) 
    
        
        # Use the x range of the canvas 
        for x in range(canvas_width): 
            # Use the y range of the canvas 
            for y in range(canvas_height): 
                # Get a pixel from the canvas, pic1, and pic2 
                canvas_pixel = media.getPixel(new_canvas, x, y)
                pic1_pixel = media.getPixel(pic1, x, y)
                pic2_pixel = media.getPixel(pic2, x, y) 
    
                # Blending magic!
                #  We take a portion of each picture color channel and combine them.
                #  You can change these values if you want to increase the intensity
                #  of one picture more than another (.50/.50 would be half of each image 
                new_red = 0.70*media.getRed(pic1_pixel) + 0.30*media.getRed(pic2_pixel)
                new_green = 0.70*media.getGreen(pic1_pixel) + 0.30*media.getGreen(pic2_pixel)
                new_blue = 0.70*media.getBlue(pic1_pixel) + 0.30*media.getBlue(pic2_pixel) 
                
                # Change the color of the canvas pixel to the blended pixel of pic1 and pic2 
                media.setColor(canvas_pixel, media.makeColor(new_red, new_green, new_blue))
        
        return new_canvas 
    
    # Take a deep breath, this is the most code we've seen so far.
    #   We could absolutely break this down into smaller functions
    #   but for now we'll focus on the book's approach
    
    # This function will take two pictures as input and blend them together
    #   however this function only blends portions of the images that overlap. 
    def partial_blend(pic1, pic2): 
            
        # Get the dimensions of the first picture 
        pic1_width = media.getWidth(pic1)
        pic1_height = media.getHeight(pic1) 
    
        # Get the dimensions of the second picture 
        pic2_width = media.getWidth(pic2)
        pic2_height = media.getHeight(pic2) 
        
        # This is the portion (width) of the first image that will NOT
        #   be blended with the second image 
        unblended_amount = 300 
        
        # This is the portion of the two images that will overlap 
        overlap = pic1_width - unblended_amount 
        
        # Create a new image that is at least as wide as both images
        #    (less the amount that overlaps) and is only as tall as
        #    the image with the largest height 
        new_canvas = media.makeEmptyPicture(pic1_width + pic2_width - overlap,
                                            max(pic1_height, pic2_height)) 
        
        # Copy the the portion of the first image that we do NOT
        #  want to have blended with the second image to our canvas 
        source_x = 0
        for target_x in range(0, unblended_amount):
            source_y = 0
            for target_y in range(0, media.getHeight(pic1)):
                color = media.getColor(media.getPixel(pic1, source_x, source_y))
                media.setColor(media.getPixel(new_canvas, target_x, target_y), color)
                source_y += 1
            source_x += 1 
        
        # Copy the rest of the first image that we want to blend
        #   with the second image 
        source_x = 0 
        # Start the range AFTER the part we have already drawn and draw the
        #  continue until we are done with the pixels in the first image 
        for target_x in range(unblended_amount, media.getWidth(pic1)):
            source_y = 0 
            
            # Our images might be different sizes, so we need to make sure we only
            #   copy the height of the smallest image so we do not get indexes
            #   outside of either image 
            for target_y in range(min(media.getHeight(pic1), media.getHeight(pic2))): 
                # We start getting pixels from pic1 after the unblended portion we
                #   have already drawn. 
                pic1_pixel = media.getPixel(pic1, source_x + unblended_amount, source_y) 
                
                # We haven't drawn any of pic2 yet so we can start copying at the
                #   beginning of the image 
                pic2_pixel = media.getPixel(pic2, source_x, source_y) 
                
                # Blending magic!
                #  We take half of each picture color channel and combine them. 
                new_red = 0.50*media.getRed(pic1_pixel) + 0.50*media.getRed(pic2_pixel)
                new_green = 0.50*media.getGreen(pic1_pixel) + 0.50*media.getGreen(pic2_pixel)
                new_blue = 0.50*media.getBlue(pic1_pixel) + 0.50*media.getBlue(pic2_pixel) 
                
                # We take those blended color values and draw them on our canvas
                color = media.makeColor(new_red, new_green, new_blue)
                media.setColor(media.getPixel(new_canvas, target_x, target_y), color)
                
                source_y = source_y + 1
            source_x = source_x + 1 
         
        # Now we need to copy the rest of pic2
        #  We have to start copying pic2 after the part that we have already blended   
        source_x = overlap 
        
        # Once again we have to start drawing on the canvas after the pixels we have
        #   already edited. This is just math based on how much we have drawn and
        #   how much the images overlap 
        for target_x in range(unblended_amount+overlap, unblended_amount+media.getWidth(pic2)):
            source_y = 0 
            
            # This time we are only drawing pic2 so we can simply use pic2's height 
            for target_y in range(0,media.getHeight(pic2)):
                color = media.getColor(media.getPixel(pic2, source_x, source_y))
                media.setColor(media.getPixel(new_canvas, target_x, target_y), color)
                
                source_y = source_y + 1
            source_x = source_x + 1
        
        return new_canvas 
    
    # Simple function to demo the drawing functions
    #   that media supplies 
    def drawing():
        
        new_canvas = media.makeEmptyPicture(800, 600) 
        
        ##### LINE ###################### 
        # Parameters: picture, x1, y1, x2, y2, color (optional) 
        media.addLine(new_canvas, 0, 0, 100, 100)
        media.addLine(new_canvas, 0, 100, 200, 50, media.green) 
    
        ##### TEXT ######################
        # Parameters: picture, x, y, text string, color (optional) 
        media.addText(new_canvas, 300, 10, 'Drawing Examples')
        media.addText(new_canvas, 300, 30, 'FUN TIME!', media.magenta) 
        
        # Parameters: font_name, emphasis, size
        #    font_name options: media.sansSerif, media.serif, media.mono
        #    emphasis options: media.italic, media.bold, media.plain 
        font_style = media.makeStyle(media.mono, media.italic, 24) 
    
        # Parameters: picture, x, y, text string, style, color (optional) 
        media.addTextWithStyle(new_canvas, 400, 550, "Fancy Text!", font_style, media.blue) 
        
        ##### SQUARE/RECTANGLE ##########
        # Parameters: picture, x, y, w, h, color (optional) 
        media.addRect(new_canvas, 300, 50, 60, 90)
        media.addRect(new_canvas, 375, 50, 60, 90, media.magenta)
        media.addRectFilled(new_canvas, 300, 160, 50, 50)
        media.addRectFilled(new_canvas, 375, 160, 100, 100, media.yellow) 
        
        ##### CIRCLE/OVAL ############### 
        # Parameters: picture, x, y, w, h, color (optional) 
        media.addOval(new_canvas, 575, 150, 90, 90)
        media.addOval(new_canvas, 675, 200, 100, 100, media.orange)
        media.addOvalFilled(new_canvas, 600, 50, 70, 70)
        media.addOvalFilled(new_canvas, 700, 10, 70, 150, media.cyan) 
        
        ##### ARC #######################
        # Parameters: picture, x, y, w, h, start_angle, end_angle, color (optional)
        #   Angles are formed counter clockwise from starting angle
        # 
        #        180 --------- 0
        #
        #   width and height parameters skews the arc 
        media.addArc(new_canvas,500, 350, 120, 120, 0, 90)
        media.addArc(new_canvas, 150, 350, 100, 100, 180, 90, media.blue)
        media.addArcFilled(new_canvas, 400, 350, 100, 100, 0, 180)
        media.addArcFilled(new_canvas, 250, 350, 100, 100, 45, 90, media.green)
    
        return new_canvas
    
    lizard = media.makePicture('images/lizard.jpg')
    ostrich = media.makePicture('images/ostrich.jpg') 
    
    # Crop the lizards face and scale it up x3
    #  and blur the image to hide the pixelization 
    media.show(lizard)
    face = scale_up_region(lizard, 252, 82, 364, 169, 3)
    print("Write blurred lizard to file")
    media.writePictureTo(blur(face), 'out.jpg')
    media.show(face) 
    
    # Bluring Example #2 (Easier to see) 
    print("Write blurred ostrich to file")
    media.writePictureTo(blur(ostrich), 'out.jpg')
    media.show(ostrich) 
    
    # Blending one image on top of another 
    wall = media.makePicture('images/wood_wall.jpg') 
    rainbow = media.makePicture('images/rainbow.jpg')
    media.show(wall)
    media.show(rainbow)
    media.show(basic_blend(wall, rainbow)) 
    
    # Partially overlapping images blended together 
    parrot = media.makePicture('images/parrot.jpg')
    forest = media.makePicture('images/forest.jpg')
    media.show(forest)
    media.show(parrot)
    media.show(partial_blend(forest, parrot)) 
    
    # Example of using the drawing functions in media 
    media.show(drawing())
    
    media.quit()