1.0 Selectively Change Colors

1.1 Remove Red Eye

Play with the removeRedEye.py example from the text, to become comfortable with selecting x-y ranges in which colors will be replaced. Call the function with the tree frog photo and the cottontail rabbit.

For the tree frog, try also removing the red from the frog'sfeet rather than from the eye.

For both photos, what range of values for the distance() function work? How small a 'distance' between the color of the pixel to change and the comparison color is required for this function to work? If you change the sensitivity of the comparison ( i.e., use a number other than '165' for the conditional statement), can you process the entire picture through your function (rather than just the rectangle around the eye or the foot)?

# removeRedEye.py

# Function to remove the red-eye effect from a photo

def removeRedEye(pic, startX, startY, endX, endY, newColor):

for x in range(startX, endX):

for y in range(startY, endY):

currentPx = getPixel(pic, x, y)

# text solution is for the color comparison to be < 165

if (distance(red, getColor(currentPx)) < 165):

setColor(currentPx, newColor)

show(pic)

3.2 Remove Blue ... only from some pixels

We saw an example of selectively changing colors in class - but only on the board! That example changes the blue of a garage, without changing the white paint for the trim on the garage. That example used both for-loops and if-else statements, as well as the distance() function used to compare the mathematical 'distance' between the color values of two pixels. The two versions of remove blue from the garage are below - the first sets the blue value of every pixel to 0 and the second zeros the blue value only of those pixels that appear 'blue.' Test these functions to make sure you get the expected results, and to make sure you understand how they work.

  • Recall that in order to obtain the (x, y) coordinates for the upper left and lower right corners of the rectangle to use, you need to create the picture in JES, and then open the Picture Tools... from the MediaTools menu.
  • What are the expected results from the first and second functions below?
  • Do you get these expected results?
  • Change the number '50' in the line "if colDiff < 50" until you get the expected result (that the white trim stays white while the blue is removed from the rest of the garage)

# noBlueInArea.py

# Functions to remove blue from a specified area of a picture

# i.e., Set the blue value to 0 for pixels in the rectangle defined by

# (x1, y1) to (x2, y2)

# operate on all pixels in the range

def noBlueArea(picture, x1, y1, x2, y2):

for x in range(x1, x2+1):

for y in range (y1, y2+1):

px = getPixel(picture, x, y)

setBlue(px, 0)

show(picture)

# set blue value to 0 in the defined rectangle only if the overall

# color of thepixel is predominantly blue - thus, for example,

# leaving white unchanged

def noBlueArea2(picture, x1, y1, x2, y2):

for x in range(x1, x2+1):

for y in range (y1, y2+1):

px = getPixel(picture, x, y)

col = getColor(px)

colDiff = distance(col, blue)

if colDiff < 50:

setBlue(px, 0)

show(picture)

Solution Note:

It is important for you to play with different values for the condition in the line "if colDiff < 50:" and find one that works. The value that worked for me was 225. You will need to use different values depending upon what you are comparing and upon the effect you are hoping to obtain. This process is important for the homework.

Trial and Error

Note that with the color comparisons and selectively changing colors, there is a fair amount of trial and error involved. You need to process only specified sections of your picture, and you need to try different values for the comparison of colors, such as in the 'if collDiff < 50" line above. You will be doing a lot of this for the homework as well. Be patient and have fun!