Home Schedule
Examples of using media.distance and conditionals. Download Complete Example
    
    import media

    # Check color distances
    def paint_red_door():
        filename = 'redDoor.jpg'

        picture = media.makePicture(filename)
        media.explore(picture)
        
        for pixel in media.getPixels(picture):
            # Check how close a pixels color is to red (must be within a distance of 150)
            if media.distance(media.getColor(pixel), media.red) < 150:
                media.setRed(pixel, media.getRed(pixel) * .2)
                media.setGreen(pixel, media.getGreen(pixel) * 2)
                media.setBlue(pixel, media.getBlue(pixel) * 2)
                
        media.explore(picture)

    # Check color distances within a certain range of pixels
    def paint_red_door_region():
        filename = 'redDoor.jpg'

        picture = media.makePicture(filename)
        media.explore(picture)

        for pixel in media.getPixels(picture):
            x = media.getX(pixel)
            y = media.getY(pixel)
            
            # Make sure the pixel is within a certain region of the image
            if 153 <= x <= 284 and 80 <= y <= 375:
                # Check how close a pixels color is to red (must be within a distance of 200)
                if media.distance(media.getColor(pixel), media.red) < 200:
                    media.setRed(pixel, media.getRed(pixel) * .2)
                    media.setGreen(pixel, media.getGreen(pixel) * 2)
                    media.setBlue(pixel, media.getBlue(pixel) * 2)
                
        media.explore(picture)

    # Red Eye Example using more complex checking for each eye
    def remove_red_eye():
        filename = 'red-eye.jpg'

        picture = media.makePicture(filename)
        media.explore(picture)

        for pixel in media.getPixels(picture):
            x = media.getX(pixel)
            y = media.getY(pixel)
            
            # Make sure the pixel is within a certain region of the image
            #                LEFT EYE                                  RIGHT EYE
            if (160 <= x <= 196 and 197 <= y <= 232) or (459 <= x <= 496 and 196 <= y <= 234):
                if media.distance(media.getColor(pixel), media.red) < 200:
                    media.setRed(pixel, 0)
                    media.setGreen(pixel, 0)
                    media.setBlue(pixel, 0)
                    
        media.explore(picture)

    # Helper function for sepia tone
    def convert_gray_scale(picture):
        for pixel in media.getPixels(picture):
            red = media.getRed(pixel) * 0.299
            green = media.getGreen(pixel) * 0.587
            blue = media.getBlue(pixel) * 0.114
            
            luminance = red + green + blue
            media.setColor(pixel, media.makeColor(luminance, luminance, luminance))

    # Give a photo an Old Timey yellowing look
    def convert_sepia():
        filename = 'country-side.jpg'

        picture = media.makePicture(filename)
        media.show(picture)
        
        # Need a gray scale image first
        convert_gray_scale(picture)
        media.show(picture)
        
        for pixel in media.getPixels(picture):
            red = media.getRed(pixel)
            blue = media.getBlue(pixel)
            
            # Check for ranges of red and tint with blue and red
            #   Color values were provided by book and could be tuned 
            if red < 63:
                red = red * 1.1
                blue = blue * 0.9
            elif 62 < red < 192:
                red = red * 1.25
                blue = blue * 0.85
            else:
                red = red * 1.08
                blue = blue * 0.93
            
            media.setBlue(pixel, blue)
            media.setRed(pixel, red)
                
        media.show(picture)
        
    # Reduce and images possible color space
    def posterize():
        filename = 'mccaw.jpg'

        picture = media.makePicture(filename)
        media.show(picture)
        
        for pixel in media.getPixels(picture):
            red = media.getRed(pixel)
            green = media.getGreen(pixel)
            blue = media.getBlue(pixel)
            
            # Reduce color space to a range of 4 possible values
            #check and set red values
            if(red < 64):
            media.setRed(pixel, 31)
            if(red > 63 and red < 128):
            media.setRed(pixel, 95)
            if(red > 127 and red < 192):
            media.setRed(pixel, 159)
            if(red > 191 and red < 256):
            media.setRed(pixel, 223)
            
            #check and set green values
            if(green < 64):
            media.setGreen(pixel, 31)
            if(green > 63 and green < 128):
            media.setGreen(pixel, 95)
            if(green > 127 and green < 192):
            media.setGreen(pixel, 159)
            if(green > 191 and green < 256):
            media.setGreen(pixel, 223)
            
            #check and set blue values
            if(blue < 64):
            media.setBlue(pixel, 31)
            if(blue > 63 and blue < 128):
            media.setBlue(pixel, 95)
            if(blue > 127 and blue < 192):
            media.setBlue(pixel, 159)
            if(blue > 191 and blue < 256):
            media.setBlue(pixel, 223)
            
        media.show(picture)
        

    paint_red_door()
    paint_red_door_region()
    remove_red_eye()
    convert_sepia()
    posterize()

    media.quit()