使用Python中的for循环垂直翻转图像 [英] Flipping an image vertically using for loops in Python

查看:137
本文介绍了使用Python中的for循环垂直翻转图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试垂直翻转图像,但是保存的图像最终变为同一张图像.我认为做new_image.set_pixel(height-1)-r ....会将像素发送到转置的垂直部分.关于我做错了什么,您可以给我一些指导吗?

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)

推荐答案

在最后一行中,您需要:

In your last line you need:

new_img.save(new_filename)

按现在的样子,您正在保存原始版本的img.

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

这篇关于使用Python中的for循环垂直翻转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆