Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
A Pattern for Image Processing¶
As we have seen with turtles and words, there are some general patterns in the programs that we write. With turtles, there was a polygon pattern (based on the Total Turtle Trip Theorem). When working with words and numbers, we used the accumulator pattern.
The 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 are our six steps:
STEP 1: USE THE IMAGE LIBRARY. We need to import the image library.
STEP 2: PICK THE IMAGE. We pick a particular image from our library by specifying it inside of the parentheses and double quotes.
STEP 3: LOOP THROUGH THE PIXELS This example gets every pixel in the image and loops through them all one at a time.
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.
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.
STEP 6: UPDATE THE IMAGE. This will update the image at the original pixel location to the new color.
STEP 7: SHOW THE RESULT. This will draw the changed image in a window.
Mixed Up Code Problems
The problem below has a Help Me button, but it starts out disabled. You must make at least 3 attempts to solve this problem before the button becomes enabled. You can click on the Help Me button when it is enabled to make the problem easier.
The program below should set all the red values to half the blue values. Drag the needed code blocks below from the left to the right in the correct order with the correct indention. There may be extra blocks that are not needed in a correct solution. Click on the Check Me button to check your solution.
The program below should set all the green values to half the red values. Drag the needed code blocks below from the left to the right in the correct order with the correct indention. There may be extra blocks that are not needed in a correct solution. Click on the Check Me button to check your solution.