PROBLEM 1: Let's reflect on life
Function Name: mirror
Parameter: filename – a string that contains the name of the
picture file Return value: picture – picture that has been
modified
Description:
When reflecting on these past few weeks, life has been pretty darn stressful but at least Fall is starting so better weather! Yay! I'm so excited for pumpkins and the spooky season! Anyways, speaking of reflecting... write a function that mirrors the picture horizontally (left to right) and then mirrors it again vertically (bottom to top). (HINT: It's helpful to use an empty canvas to start.)
Test Cases:
>>> show(mirror("buzz.jpg"))
If you are allowed to use libraries, you can use this:
from PIL import Image
imgObj = Image.open("images/img")
horiFlipped = imgObj.transpose(Image.FLIP_LEFT_RIGHT)
vertiFlipped = horiFlipped.transpose(Image.FLIP_TOP_BOTTOM)
vertiFlipped.show()
If you are not allowed and want to reinvent the wheel: (Horizontal flip)
def flip(img):
width = img.size[0]
height = img.size[1]
for y in range(height):
for x in range(width//2):
left = img.getpixel((x, y))
right = img.getpixel((width - 1 - x, y))
img.putpixel((width - 1 - x, y), left)
img.putpixel((x, y), right)
Source: StackOverflow
You can comment on this if you need more or anything else. Happy to help :)
Get Answers For Free
Most questions answered within 1 hours.