n my function there are the two options: flipping vertically or horizontally. vertically if vertically = True. The horizontal part is correct however, when i try to flip an image vertically, the image i get ends up being just 42% flipped. I don´t know which part of my code is wrong. Can you give me some guidance?
def flip(image,vertical=False):
height = len(image)
width = len(image[0])
for row in range(height):
for col in range(width//2):
pixel = image[row][col]
image[row][col] = image[row][width - col - 1]
image[row][width - col - 1] = pixel
if vertical == True:
image[row][col] = image[height-row-1][width - col - 1]
image[height-row-1][width - col - 1] = pixel
return True
Try changing the penultimate line from
to
However, it's probably better to rethink the algorithm and either flip horizontally or flip vertically. The method in the question flips horizontally then, if vertical is True, tries to undo the horizontal flip and simultaneously do a vertical flip (and, I think, misses the fact that 'pixel' is from the unflipped coordinate system). This makes the code difficult to read.
An even better solution would be to reverse with the slice notation:
or, for the other axis:
(I'm answering on my phone so can't check the code, apologies for any typos)