Section 18.4 Changing Step 5: Increasing and decreasing color values
First example: Let's change STEP 5, so that we decrease the red by 50%.
Try the program above on some of the other images, like “arch.jpg” or “puppy.jpg”, by changing the name of the image file on line 5. What effect does it always have? Is this what you expected when we decrease the red?
We can increase the red in a similar way. Let's change STEP 5, so that we increase the red by 150%.
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 you increase the red? What happens if you increase two colors at the same time?
Checkpoint 18.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 image import *
---
img = Image("puppy.jpg")
---
pixels = img.getPixels()
---
for p in pixels:
---
g = p.getGreen()
b = p.getBlue()
---
p.setGreen(g * 0.75)
p.setBlue(b * 0.75)
---
img.updatePixel(p)
---
win = ImageWin(img.getWidth(),img.getHeight())
img.draw(win)
Checkpoint 18.4.2.
Solution.# STEP 1: USE THE IMAGE LIBRARY
from image import *
# STEP 2: PICK THE IMAGE
img = Image("puppy.jpg")
# STEP 3: LOOP THROUGH THE PIXELS
pixels = img.getPixels()
for p in pixels:
# STEP 4: GET THE DATA
r = p.getRed()
g = p.getGreen()
b = p.getBlue()
# STEP 5: MODIFY THE COLOR
p.setRed(r * .5)
p.setGreen(g * 1.5)
p.setBlue(b * 1.5)
# STEP 6: UPDATE THE IMAGE
img.updatePixel(p)
# STEP 7: SHOW THE RESULT
win = ImageWin(img.getWidth(),img.getHeight())
img.draw(win)