题目
你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。
如果只是单纯的通过将图片缩放到iPhone5分辨率大小,显然最后呈现出来的效果会很糟糕。所以等比例缩放到长(或宽)一定时,再将宽(或长)进行裁剪
代码
from PIL import Image
import os
def ZoomImage(dir,filename):
IMG1=Image.open(dir)
width,height=IMG1.size
if (width/height)<=(i5_width/i5_height):
# 缩放到宽=iphone5的宽度
IMG1=IMG1.resize((i5_width,int(height*(i5_width/width))),Image.ANTIALIAS)
else:
IMG1=IMG1.resize((int(width*(i5_height/height)),i5_height),Image.ANTIALIAS)
# 裁剪
width,height=IMG1.size
x=(width-i5_width)//2
y=(height-i5_height)//2
h=i5_height+y
w=i5_width+x
IMG2=IMG1.crop((x,y,w,h))
IMG2.save('E:/Converted/'+filename)
def file_name(file_dir):
for root, dirs, files in os.walk(file_dir):
for file in files:
ZoomImage(root+'/'+file,file)
if __name__ == '__main__':
i5_width=640
i5_height=1136
file_dir="E:/Picture"
file_name(file_dir)