08ColorReplacements

# 08ColorReplacements - CS1315: Introduction to Media...

This preview shows pages 1–9. Sign up to view the full content.

CS1315: Introduction to Media Computation Color replacements and targeted color replacement (if statement)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Let’s try making Barbara a redhead! We could just try increasing the redness, but as we’ve seen, that has problems. Would change all pixels And that’s more than just her hair If only we could increase the redness only of the brown areas of Barb’s head… Need a way to make sure the pixel we alter is a brown one!
Treating pixels differently We can use the if statement to treat some pixels differently That’s “conditional execution” For example, color replacement: only changing greens to yellows Use the MediaTools to find the RGB values for the part you want to change Then check if a pixels is close to that color (within a threshold ), and modify it if so.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
How “close” are two colors? Sometimes you need to find the distance between two colors, e.g., when deciding if something is a “close enough” match How do we measure distance? Pretend it’s Cartesian coordinate system Distance between two points: Distance between two colors:
Making Barb a redhead def turnRed( ): brown = makeColor (57, 16, 8) file = r"C:\My Documents\MediaSources\barbara.jpg" picture = makePicture (file) for pixel in getPixels (picture): color = getColor (pixel) if distance(color, brown) < 50.0: redness = getRed (pixel)*1.5 setRed (pixel, redness) show (picture) return (picture) Original: Digital makeover:

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Talking through the program slowly Why no parameters? Don’t need any: this recipe is specific to the Barbara one picture. The brown is the brownness from MediaTools using that picture The file is where the picture of Barbara is on the computer We need exactly that picture - so some recipes might be dedicated like this def turnRed( ): brown = makeColor (57, 16, 8) file = r"C:\My Documents\MediaSources\barbara.jpg" picture = makePicture (file) for pixel in getPixels(picture): color = getColor(pixel) if distance(color, brown) < 50.0: redness = getRed(pixel)*1.5 setRed(pixel, redness) show(picture)
def turnRed( ): brown = makeColor (57, 16, 8) file = r"C:\My Documents\MediaSources\barbara.jpg" picture = makePicture (file) for pixel in getPixels(picture): color = getColor(pixel) if distance(color, brown) < 50.0: redness = getRed(pixel)*1.5 setRed(pixel, redness) show(picture) Walking through the for loop Now, for each pixel pixel in the picture get the color see if it’s within a distance of 50 from the brown we want to make more red if so, increase the redness by 50%

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
How if works if is the command name an expression: some kind of True or False comparison a colon the body of the if —the things that will happen if
This is the end of the preview. Sign up to access the rest of the document.

## 08ColorReplacements - CS1315: Introduction to Media...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online