9.4. A Pattern for Image Processing

There is a fair amount of code involved in a program like this. But there is a pattern to the code we need to write. Learning to recognize the pattern makes it easier to write a new program - we can follow the steps of the pattern and only worry about modifying small parts of the basic recipe.

A basic image processing pattern is shown in the program below. This program changes the red to the original green, the green to the original blue, and the red to the original green. But, mostly we are trying to describe a pattern that you can use to create many image effects.

Here is the recipe without the code:

  1. STEP 1: USE THE IMAGE LIBRARY. We need to import the image library.

  2. STEP 2: PICK THE IMAGE. We pick a particular image from our library by specifying

    it inside of the parentheses and double quotes.

  3. STEP 3: LOOP THROUGH THE PIXELS. This example gets every pixel in the image and

    loops through them all one at a time.

  4. STEP 4: GET THE DATA. You could always use the STEP 4 lines just as they are above,

    but you might be able to make it shorter if you wanted. If you only needed red and were going to set the green and blue to zero, you don’t have to get the green and blue.

  5. STEP 5: MODIFY THE COLOR. This is the part that you will most often change. Here’s where

    you change the red, green, and/or blue. You don’t have to change all of them.

  6. STEP 6: UPDATE THE IMAGE. This will update the image at the original pixel location to the new color.

  7. STEP 7: SHOW THE RESULT. This will draw the changed image in a window.

Time to experiment with our pattern.

You have attempted of activities on this page