08ColorReplacements

08ColorReplacements - CS1315: Introduction to Media...

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

View Full Document Right Arrow Icon
CS1315: Introduction to Media Computation Color replacements and targeted color replacement (if statement)
Background image of page 1

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

View Full DocumentRight Arrow Icon
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!
Background image of page 2
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.
Background image of page 3

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

View Full DocumentRight Arrow Icon
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:
Background image of page 4
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:
Background image of page 5

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

View Full DocumentRight Arrow Icon
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)
Background image of page 6
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%
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 31

08ColorReplacements - CS1315: Introduction to Media...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online