第4题:有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。
#!/usr/bin/env python3
# -*- coding : utf-8 -*-
from PIL import Image
import os
def changeimgResolution(dirpath):
for filename in os.listdir(dirpath):
imgpath = os.path.join(dirpath,filename)
with Image.open(imgpath) as im:
width,height = im.size
if width > 640 or height > 1136:
wh = width / 640 if(width / 640) >= (height / 1136) else height / 1136
out = im.resize((int(width / wh),int(height / wh)))
#im.save(imgpath)
out.save(os.path.join(dirpath,'ok_' + filename.split('.')[0] + '.jpg'),'jpeg')
print('Successfully resized %s.' % imgpath)
if __name__ == '__main__':
dirpath = input('Please input dirpath:')
changeimgResolution(dirpath)
本文介绍了一个使用Python编写的脚本,该脚本能批量处理指定文件夹内的所有图片,将其分辨率调整为适合iPhone5屏幕大小。具体操作包括读取图片尺寸、判断是否需要缩放以及按比例调整图片大小。

被折叠的 条评论
为什么被折叠?



