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()
手机扫一扫
移动阅读更方便
你可能感兴趣的文章