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 colors around in crazy ways. It sets the red value to the original green, the green to the original blue, and the blue to the original red. Compare the results of running this program with the original goalkeeper image from the Image Library.
PICK THE IMAGE AND DISPLAY IT AND DISPLAY IT. We pick a particular image from our library by specifying its name as a string (with quotation marks) inside the parentheses.
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 don’t need all the colors. If your program just needs to work with red, you don’t have to get the green and blue values.
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.
Notice that steps 4-6 are indented in the code. That means they are part of the for loop on line 9. They get repeated for each of the pixels in the image. Step 7 is not indented. It happens just once after all the pixels are processed in the for loop.
Change lines 17-19 so that the new red value is 255 and blue and green are each 0. Run the program and observe the results. Then change the red value to 300 and the blue to -100. Run it again. What seems to happen?