Section18.6Changing Step 3: Changing which data we use
Below is a selection of images that you can use in the programs in this section.
We can also change which part of the picture we read and manipulate. When we are changing several colors at once we can create a new pixel with the desired color using Pixl(red,green,blue) as shown below on line 20.
What happens if we skip every other x and y as we manipulate the colors? Maybe make the green 255 and the blue 0?
Let's try side-to-side copying.
Checkpoint18.6.1.
Q-4: Try it: How would you mirror the image from left-to-right around a vertical line in the middle of the picture? Try changing line 22 to these. If you get it right it will look like the women is nose to nose with herself.
# USE THE IMAGE LIBRARY
from image import *
# PICK THE IMAGE
img = Image("vangogh.jpg")
# SELECT THE DATA
halfwayWidth = (int) (img.getWidth() / 2)
halfwayHeight = (int) (img.getHeight() / 2)
for x in range(halfwayWidth):
for y in range(halfwayHeight):
# GET THE DATA
p = img.getPixel(x, y)
r = p.getRed()
g = p.getGreen()
b = p.getBlue()
# CREATE THE COLOR
newPixel = Pixel(r, g, b)
# CHANGE THE PIXEL
img.setPixel(halfwayWidth + x, halfwayHeight + y, newPixel)
# SHOW THE RESULT
win = ImageWin(img.getWidth(),img.getHeight())
img.draw(win)