Home Schedule
Examples of using nested looping to more efficiently perform picture manipulations. Download Complete Example

# Import needed only for the timings
import time
import media

# NESTED LOOP EXAMPLE
def nested_loop_test():
    # The outer loop executes 4 times (0 - 3) 
    for outer_number in range(4):
        print("outer loop:", outer_number) 
        
        # The inner loops executes 3 times (0-2)
        #     for each time the outer loop runs (4 * 3)
        #     12 times total
        for inner_number in range(3):
            print("\tinner loop:", inner_number) 
            
        # Separate each run of the outer loop
        print('---------------------------') 

# SLOW WAY
#   Remove color from a specific section of an image
def slow_remove_color(image, tl_x, tl_y, br_x, br_y):
    new_image = media.duplicatePicture(image)
    
    for pixel in media.getPixels(new_image):
        x = media.getX(pixel)
        y = media.getY(pixel)
        
        if tl_x <= x <= br_x and tl_y <= y <= br_y:
            pixel = media.getPixel(new_image, x, y)
            
            red = media.getRed(pixel)
            green = media.getGreen(pixel)
            blue = media.getBlue(pixel)
            lum = (red + green + blue) // 3
            
            media.setColor(pixel, media.makeColor(lum, lum, lum))

    return new_image

# FAST WAY
#   Remove color from a specific section of an image
def fast_remove_color(image, tl_x, tl_y, br_x, br_y):
    new_image = media.duplicatePicture(image)

    for x in range(tl_x, br_x):
        for y in range(tl_y, br_y):
            pixel = media.getPixel(new_image, x, y)
            
            red = media.getRed(pixel)
            green = media.getGreen(pixel)
            blue = media.getBlue(pixel)
            lum = (red + green + blue) // 3
            
            media.setColor(pixel, media.makeColor(lum, lum, lum))
            
    return new_image

# Mirror a picture vertically
def mirror_picture(image):
    new_image = media.duplicatePicture(image)
    
    width = media.getWidth(image)
    height = media.getHeight(image) 
    
    # All x values
    for x in range(width): 
        # Top half of the image y values
        for y in range(height//2): 
            # Get the pixel to replace (top half with the empty yellow) 
            dest_pixel = media.getPixel(new_image, x, y) 
            
            # Get a pixel at the same x, but at the bottom most y value
            source_pixel = media.getPixel(new_image, x, (height-1) - y) 
            
            # Replace the color of the top most pixel with the bottom most pixel
            #   (a reflection) 
            source_color = media.getColor(source_pixel)
            media.setColor(dest_pixel, source_color)
        
    return new_image

# Replace a broken door with a repaired one 
def fix_door(image):
    new_image = media.duplicatePicture(image) 
    
    # starting x position of the broken door (top left) 
    start_x = 28 
    
    # Range of x values that make up the fixed door
    #     left most and right most x values 
    for x in range(411, 541): 
        # starting y position of the broken door (top left) 
        start_y = 148 
        
        # Range of the y values that make up the fixed door
        #    top most and bottom most y values 
        for y in range (147, 519): 
            # Get the pixel from the fixed door 
            source_pixel = media.getPixel(new_image, x, y) 
            
            # Get the pixel from the broken door 
            dest_pixel = media.getPixel(new_image, start_x, start_y) 
            
            # Set the broken door pixe to the fixed door pixel 
            media.setColor(dest_pixel, media.getColor(source_pixel)) 
            
            # move down to the next broken door pixel 
            start_y += 1 

        # move right to the next broken door pixel 
        start_x += 1 
    
    return new_image 

# Example of nested loops 
nested_loop_test() 

# Change color of B button on controller 
nes_controller = media.makePicture('images/nes.jpg') 

# SLOW TEST 
start_time = time.perf_counter_ns()
edit = slow_remove_color(nes_controller, 393, 227, 440, 265)
print("seconds:", (time.perf_counter_ns() - start_time)/1E9)
media.show(edit) 

# FAST TEST 
start_time = time.perf_counter_ns()
edit = fast_remove_color(nes_controller, 393, 227, 440, 265)
print("seconds:", (time.perf_counter_ns() - start_time)/1E9)
media.show(edit) 

# Repair a broken door 
green_doors = media.makePicture('images/green-doors-small.jpg')        
media.show(green_doors)
media.show(fix_door(green_doors)) 

# Mirror the controller horizontally 
ps4_controller = media.makePicture('images/ps4.jpg')
media.show(ps4_controller)
media.show(mirror_picture(ps4_controller))

media.quit()