问题
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