O'Reilly logo

A Concise Introduction to Programming in Python by Mark J. Johnson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required