18.1. Using Repetition with Images¶
Learning Objectives:
Use
for
loops and nestedfor
loops to repeat actions on pixels in an image.Understand the pattern (the steps) used in modifying all the pixels in an image.
Describe how multiple classes (Image and Pixel) interact.
Images (pictures) on a computer are broken up into little bits called pixels, for picture (pix) elements (els). These are laid out on a grid, from left to right (horizontal or x dimension) and top to bottom (vertical or y dimension).
Pixels are quite small. Even the small image below has 180 columns and 240 rows of pixels. Each pixel has a color associated with it: an amount of redness, an amount of greenness, and an amount of blueness. Let’s remove the red from this picture. Now, there are lot of lines in the program below, but fortunately, you can ignore most of them. The Audio Tour explains the important lines. Press to hear the audio tour explanation. When you run this program it may take several minutes to show the changed image.
The program above can take several minutes to execute on the arch.jpg
picture. But we’re not stuck using just the arch image. You can
change the file name in the program above to the URL for any small photo that is on the internet.
- You still see the picture, but it is all in shades of gray.
- Not if you set all the color values to 0.
- The picture is all white.
- Did you try it? This would be true if you set all the values to 255 instead of 0.
- The picture is all black.
- Black is the absence of light so setting all colors to 0 results in an all black image since there is no light.
11-9-2: What happens when you set all the colors to 0? Try adding p.setBlue(0)
and p.setGreen(0)
to the program above after the p.setRed(0)
and run it to check.
- Yes
- It shows all the green values from none to lots so it looks like a greenish version of a grayscale image.
- No
- Did you try it?
11-9-3: What happens when you keep only the original green color? Will you still be able to see the arch?
- It is all black.
- No, this would happen if you set every value to 0.
- Shows as a grayscale image.
- Did you try it?
- Shows as a negative of the original image.
- It is a negative of the original image.
- It is all white.
- No, this would happen if you set every value to 255.
11-9-4: What happens if set every color value to 255 minus the original value?