简体   繁体   English

在Python中使用for循环垂直翻转图像

[英]Flipping an image vertically using for loops in Python

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. 我认为做new_image.set_pixel(height-1)-r ....会将像素发送到转置的垂直部分。 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)

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. 到现在为止,您正在保存img ,它是原始版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM