Changing Step 5: Increasing and decreasing color values¶
Below is a selection of images that you can use in the programs in this section.
beach.jpg | baby.jpg | vangogh.jpg | swan.jpg |
puppy.jpg | kitten.jpg | girl.jpg | motorcycle.jpg |
gal1.jpg | guy1.jpg | gal2.jpg |
First example: Let’s change Step 5, so that we set the green and blue values of each pixel equal to its red value.
Try the program above on some of the other images by changing the name of the image file on line 5. What effect does it always have? Is this what you expected when we set all three colors to the red value? Why does the image appear in black-and-white instead of color?
Change line 5 so that all three color values to the intial green value instead of the red. How does the resulting image differ? Finally, try this again setting all the colore to the blue value.
We can increase and decrease the color values for a wide range of effects.
Now let’s set the red value only to the average of the previous red, green, and blue values of the pixel and set the green and blue values to 0. Try to anticipate what the image will look like before running the program.
Did it look like you thought it would? Your ability to “be the computer” and anticipate what a program will do before you run it is a very important skill for a programmer to develop.
Change the program to set the green pixel to the previous average of all three colors while setting red and blue to 0. What happens if you set both the red and green to the pixel average and leave only the blue at 0?
Take time to experiment with different combinations, and rerun your experiments with different images.
csp-11-4-2: csp-11-4-1: Another way to get a similar effect to increasing the red, is to decrease
the green and blue. Figure out how to do that in the program above and then
use that information to drag the code blocks below from the left to the
right in the correct order with the correct indention.from PIL import Image
---
img = Image.open("beach.jpg")
---
pixels = img.load()
---
for col in range(img.size[0]):
---
for row in range(img.size[1]):
---
r, g, b = pixels[col, row]
---
pixels[col, row] = (r, g * 0.75, b * 0.75)
---
img.show()
Decrease the red by .5 and increase the blue and green by .5 in puppy.jpg.