图片按照宽度等比例缩放

资源链接:https://download.csdn.net/download/qq_35866846/12105054

图片大小调整批处理:固定宽度等比例缩放
主要库:PIL.Image

优劣对比:
PS自带批处理 2-3 张/秒
pythonPIL库 27-28张/秒

from  PIL import Image
import os,time
old_path=r"测试\\"  #原图片的存放地址
new_path=r"测试_调整\\"  #调整后图片的存放地址
pic_names=os.listdir(old_path)
width=180

def resize_by_width(old_path,new_path,pic_name,width):
    im=Image.open(old_path+pic_name)
    (x,y)=im.size
    x_s=width
    y_s=int(y*x_s/x)
    out = im.resize((x_s, y_s), Image.ANTIALIAS)
    out.save(new_path+pic_name)

start=time.time()
a,b,c=0,0,0
for pic_name in pic_names:
    a+=1
    try:
        resize_by_width(old_path,new_path,pic_name,width)
        b+=1
        print("第 %s 张图片 %s 调整完成"%(a,pic_name))
    except:
        c+=1
        print("------第 %s 张图片 %s 调整失败------"%(a,pic_name))
end=time.time()
print("共计 %s 张图片调整完成,成功 %s 张,失败 %s 张,耗时 %s 秒"%(a,b,c,(end-start)))

参考资料:
https://blog.csdn.net/ln152315/article/details/42777149

发布了35 篇原创文章 · 获赞 35 · 访问量 2526

猜你喜欢

转载自blog.csdn.net/qq_35866846/article/details/103996641