ch05 - Chapter 5: Advanced Picture Techniques 1 Chapter...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter 5: Advanced Picture Techniques 1 Chapter Objectives 2 Color replacement You remember the increaseRed recipe: 3 def increaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*1.2) And the function to calculate the distance between two colors: Suppose we wrote the function distance(color 1 ,color 2 ) to calculate the distance above Color replacement (cont.) Now, we want to make Barbs hair more red So, we need first to single out where barbs hair is in the image, that is the range of pixels we want to modify We can determine that by using the explore tool Then we need to define a reference color , and For each pixel of Barbs image falling in the range above: If the color of the pixel is close to our reference color, then we increase the red component of this pixel 4 Replacing colors in a range def turnRedInRange(): brown = makeColor(57,16,8) file=/Users/ guzdial/mediasources/barbara.jpg" picture=makePicture(file) for x in range(70,168): for y in range(56,190): px=getPixel(picture,x,y) color = getColor(px) if distance(color,brown)<50.0: redness=getRed(px)*1.5 setRed(px,redness) show(picture) return(picture) Get the range using MediaTools 5 Walking this code Lets write our function. It does not need input; it creates the color brown; it makes a picture from the file containing the image of Barb def turnRedInRange(): brown = makeColor(57,16,8) file=/Users/ guzdial/mediasources/barbara.jpg" picture=makePicture(file) for x in range(70,168): for y in range(56,190): px=getPixel(picture,x,y) color = getColor(px) if distance(color,brown)<50.0: redness=getRed(px)*1.5 setRed(px,redness) show(picture) return(picture) 6 The nested loop We used MediaTools to find the rectangle where most of the hair is that we want to change def turnRedInRange(): brown = makeColor(57,16,8) file=/Users/ guzdial/mediasources /barbara.jpg picture=makePicture(file) for x in range(70,168): for y in range(56,190): px=getPixel(picture,x,y) color = getColor(px) if distance(color,brown)<50.0: redness=getRed(px)*1.5 setRed(px,redness) show(picture) return(picture) 7 Were looking for a close-match on hair color, and increasing the redness def turnRedInRange(): brown = makeColor(57,16,8) file=/Users/ guzdial/mediasources /barbara.jpg picture=makePicture(file) for x in range(70,168): for y in range(56,190): px=getPixel(picture,x,y) color = getColor(px) if distance(color,brown)<50.0: redness=getRed(px)*1.5 setRed(px,redness) show(picture) return(picture) 8 After having modified the picture, our function returns the modified picture (see the return statement) Working on Katies Hair def turnRed(): brown = makeColor(42,25,15) file="C:/ip-book/mediasources/katieFancy.jpg" picture=makePicture(file) for px in getPixels(picture): color = getColor(px) if distance(color,brown)<50.0: redness=int(getRed(px)*2) blueness=getBlue(px) greenness=getGreen(px) setColor(px,makeColor(redness,blueness,greenness)) show(picture) return(picture) This version doubles all close reds....
View Full Document

Page1 / 49

ch05 - Chapter 5: Advanced Picture Techniques 1 Chapter...

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

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