20. Map Image

Changing the colors in a picture, pixel by pixel.

The map-image command runs a function on every pixel of an image. See the map-image documentation for the official info.

Exercises for map-image

  1. blue-only: Keep only the blue, removing red and green.
  2. black-white: Turn an image to black and white. You will need to decide how to set the threshold.
  3. grayscale: Turn an image to grayscale.
  4. green-to-white: Change only green pixels to white.

Color

There are several logical ways of combining the red, green, and blue components to get one number. The most common is averaging, but it is not the only way.

  1. Average the numbers.
  2. Use the maximum of the numbers.
  3. Use the minimum of the numbers. (?!)
  4. Other unusual math methods like the geometric mean.

Extras

Make the function color-distance: color->color, which measures the distance between two colors in the same way that distance measures the distance between two posns.

The formula for color distance would be:

$$(square root of (r1-r2)^2 + (g1-g2)^2 + (b1-b2)^2)$$

Your color-distance function can be used to change colors near green to white, so you don’t have to have perfect green to see it work.

Last modified August 18, 2023: 2022-2023 End State (7352e87)