Today: color-logic examples

> Logic-Color Examples

Grayscale (recall)

(a) Convert Color To Grayscale

# Convert pixel from color to grayscale
# - parenthesis required here
# - using int div //, regular / works too (float)
average = ( + + // 3 = average = average = average

Color If Logic

alt: red stop sign

b. Red Detect red > 100

c. Red Detect - Average

alt: redish is red value above average per pixel

d. Curb Repair 1

e. Curb Repair 2


Bluescreen Algorithm Outline

Here is the output we are going for with this bluescreen algorithm:


alt: red stop sign


alt: red part of stop sign replaced with leaves


alt: replace red pixel with pixel at same x,y from back image

Bluescreen Stop Sign

> Bluescreen Stop Sign

Solution code

def stop_leaves(front_filename, back_filename):
    """Implement stop_leaves as above."""
    image = SimpleImage(front_filename)
    back = SimpleImage(back_filename)
    for y in range(image.height):
        for x in range(image.width):
            pixel = image.get_pixel(x, y)
            average = ( + + // 3
            if >= average * 1.6:
                # the key line:
                back_pixel = back.get_pixel(pixel.x, pixel.y)
    return image

Bluescreen Monkey

If Sufficient time, in-class exercise/demo. Observe: the bluescreen algorithm depends on the colors in the main image. BUT it does not depend on the colors of the back image - the back image can have any colors it in. Try the different background-cases for the monkey.

> Bluescreen Monkey