[Python] flip an image vertically using loops in python

Discussão em 'Python' iniciado por Stack, Março 9, 2025.

  1. Stack

    Stack Membro Participativo

    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

    Continue reading...

Compartilhe esta Página