Flipping an image vertically using for loops in Python

纵饮孤独 提交于 2019-12-11 03:07:06

问题


Im trying to flip an image vertically, but the image saved ends up being the same one. I thought doing new_image.set_pixel(height-1)-r.... would have sent the pixel into the transposed vertical part. Can you give me some guidance as to what I have done wrong?

def flip_vert(filename):

img = load_image(filename)
height = img.get_height()
width = img.get_width()
new_img = Image(height, width)

for r in range(height):
    for c in range(width):
        temp = img.get_pixel(r, c)
        temp2 = new_img.get_pixel(r, c)
        new_img.set_pixel((height-1)-r,(width-1)-c,temp)

new_filename = 'flipv_test' + filename
img.save(new_filename)

回答1:


In your last line you need:

new_img.save(new_filename)

As it is written now you are saving img, which is the original version.



来源:https://stackoverflow.com/questions/37825635/flipping-an-image-vertically-using-for-loops-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!