原图
代码
from PIL import Image
def flip_image(image_path):
# 打开图片
with Image.open(image_path) as img:
# 获取图片的模式(RGB/RGBA/其他)
mode = img.mode
# 创建一个新图片,模式与原图一样,大小为原图的翻转大小
flipped_img = Image.new(mode, img.size)
# 翻转图片
for x in range(img.width):
for y in range(img.height):
# 获取原图的像素点,然后翻转后放到新图片对应位置
flipped_img.putpixel((x, y), img.getpixel((img.width - x - 1, y)))
# 保存翻转后的图片
flipped_img.save('flipped_' + image_path)
# 使用函数翻转图片
flip_image('your_image.jpg')