python3 批量缩放图片为iphone5的640*1136以下
try:
from PIL import Image, ImageDraw, ImageFont, ImageEnhance
except ImportError:
import Image, ImageDraw, ImageFont, ImageEnhance
import os
myPath = './srcimg/'
outPath = './destimg/'
def processImage(filesource,destsource,name,imgtype):
'''
:param filesource: 存放待转换图片的目录
:param destsource: 存放转换输出后的图片目录
:param name: 文件名
:param imgtype: 文件类型
:return:
'''
imgtype = 'jpeg' if imgtype == '.jpg' else 'png'
im = Image.open(filesource+name)
#缩放比例
rate = max(im.size[0]/640.0 if im.size[0] > 640 else 0,im.size[1]/1136.0 if im.size[1] > 1136 else 0)
if rate:
im.thumbnail((im.size[0]/rate,im.size[1]/rate))
im.save(destsource+name,imgtype)
def run():
os.chdir(myPath)
for i in os.listdir(os.getcwd()):
print(i)
postfix = os.path.splitext(i)[1]
if postfix == '.jpg' or postfix == '.png':
print(myPath,outPath,i,postfix)
processImage('./','../'+outPath,i,postfix)
if __name__ == '__main__':
# processImage(myPath, outPath, '1.jpg', '.jpg')
run()
文章对您有帮助,开心可以打赏我,金额随意,欢迎来赏!
需要电子方面开发板/传感器/模块等硬件可以到我的淘宝店逛逛