Chapter 21
Image Manipulation
Like sounds, digital images are discretized versions of their analog coun-
terparts. 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 ...
Get A Concise Introduction to Programming in Python now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.