Home Schedule
Examples of swapping backgrounds using chromakey and editing regions of an image. Download Complete Example
    
    import media

    # Chromakey example with a blue background
    def make_chromakey(pic, bg):
        picture = media.duplicatePicture(pic)
        
        for px in media.getPixels(picture):
            x = media.getX(px)
            y = media.getY(px)
            
            # Check if the sum of the red and green channels is less than
            #    the blue channel (must be significantly blue) 
            if (media.getRed(px) + media.getGreen(px) < media.getBlue(px)):
                bgpx = media.getPixel(bg, x, y)
                media.setColor(px, media.getColor(bgpx))

        return picture

    # Example of Green Chromakey to create advice dog meme 
    def make_meme(pic, bg):
        picture = media.duplicatePicture(pic)
        
        for px in media.getPixels(picture):
            x = media.getX(px)
            y = media.getY(px)
            if (media.getRed(px) + media.getBlue(px) < media.getGreen(px)):
                bgpx = media.getPixel(bg, x, y)
                media.setColor(px, media.getColor(bgpx))

        return picture 

    # More advanced example of editing regions of an image
    #   by editing the quarters of the image instead of just
    #   half (the book example) 
    def quarters(pic):
        picture = media.duplicatePicture(pic)
        
        width = media.getWidth(pic)
        height = media.getHeight(pic)
        
        for px in media.getPixels(picture):
            x = media.getX(px)
            y = media.getY(px)
            
            # Top left quadrant 
            if 0 <= x <= width//2 and 0 <= y <= height//2:
                media.setRed(px, media.getRed(px) * .5) 
            # Top right quadrant 
            elif width//2 <= x <= width - 1 and 0 <= y <= height//2:
                media.setGreen(px, media.getGreen(px) * .5) 
            # Bottom left quadrant 
            elif 0 <= x <= width//2 and height//2 <= y <= height - 1:
                media.setBlue(px, media.getBlue(px) * .5) 
            # Bottom right (the rest) quadrant 
            else:
                lum = media.getRed(px) + media.getGreen(px) + media.getBlue(px)
                lum = lum // 3
                media.setColor(px, media.makeColor(lum, lum, lum))

        return picture        

    # Book Chromakey example with different background 
    mark_chroma = media.makePicture('blue-mark.jpg')
    fine_bg = media.makePicture('everything-is-fine.jpg')
    media.show(mark_chroma)
    media.show(make_chromakey(mark_chroma, fine_bg)) 

    # Using Chromakey to generate a meme 
    meme_dog = media.makePicture('advice_dog.png')
    meme_bg = media.makePicture('meme_bg.jpg')
    media.show(meme_dog)
    media.show(make_meme(meme_dog, meme_bg)) 

    # Edit an image in quadrants
    #   More advanced example based on the book 
    flowers = media.makePicture('flowers.jpg')
    media.show(flowers)
    media.show(quarters(flowers))

    media.quit()