Chapter 21

Image Manipulation

Like sounds, digital images are discretized versions of their analog counterparts. Regular samples of color (instead of air pressure) are stored in a two-dimensional grid (instead of a one-dimensional array). Access to these samples allows us to change them in a variety of ways.

The following program requires image.py from Listing 24.3 to be in the same folder or directory.

Listing 21.1: Two-Tone Image

 1 # twotone.py
 2
 3 from image import ImagePPM
 4
 5 def luminance(c):
 6 r, g, b = c
 7 return 0.2 * r + 0.7 * g + 0.1 * b
 8
 9 def twotone(c, bright, cutoff, dark):
10 return bright if luminance(c) > cutoff else dark
11
12 def f(c):
13 return twotone(c, (255, 255, 255), 110, (0, 30, 70))
14
15 def applyfn(f, ...

Get A Concise Introduction to Programming in Python now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.