python study image mirror

安装PIL

1
pip install Pillow

单个图片

1
2
3
4
from PIL import Image,ImageOps
image = Image.open('input.png')
image_mirror = ImageOps.mirror(image)
image_mirror.save('output.png')

批量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import os
from PIL import Image, ImageOps

# 转换一个文件夹
def mirrorimage():
outputpath = "/tmp/output"
inputpath = "/tmp/input"

# 循环文件夹
for image_path in os.listdir(inputpath):
# 拼接完整路径
full_path = os.path.join(inputpath, image_path)
image = Image.open(os.path.join(inputpath, full_path))

image_mirror = ImageOps.mirror(image)
# 拼接输出路径
full_output_path = os.path.join(outputpath, 'mirror_'+image_path)
image_mirror.save(full_output_path)

if __name__ == '__main__':
mirrorimage()