Using an Image Library

Similarly, in the image processing example, we used from image import *. That made the functions getPixels() and getRed() accessible. We could also define a new function that returns a new color, or a new procedure that changes the image.

The for p in pixels on line 9 let’s us loop through all of the pixels in the image and change the red value for each pixel. We’ll talk more about looping (repeating steps) in the next chapter.

    csp-6-6-1: What does the line p.setRed(r * 0.5) do?
  • It sets the red value in the current pixel to half the red of the original.
  • Multiplying by 0.5 is the same as dividing by 2.
  • It sets the red value in the current pixel to twice the red of the original.
  • This would be true if it was r * 2, instead of r * 0.5
  • It sets the red value in the current pixel to 5 times the red of the original.
  • This would be true if it was r * 5, instead of r * 0.5
  • It sets the red value in the current pixel to 0.5.
  • This would be true if it was 0.5 instead of r * 0.5

This ability to name functions and procedures, and sets of functions and procedures, and absolutely anything and any set of things in a computer is very powerful. It allows us to create abstractions that make the computer easier to program and use. More on that in a future chapter.

Note

Discuss topics in this section with classmates.

Show Comments
Next Section - Renaming Python’s Functions