美文网首页
用 python 批量修改图片的大小

用 python 批量修改图片的大小

作者: 孙庚辛 | 来源:发表于2021-03-08 19:56 被阅读0次

    用到了 PIL 库里面的 resize 方法~

    from  PIL import Image
    import os,time
    
    old_path="created/"  #原图片的存放地址
    new_path="resized/"  #调整后图片的存放地址
    pic_names=os.listdir(old_path)
    width=512
    
    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)))
    
    

    相关文章

      网友评论

          本文标题:用 python 批量修改图片的大小

          本文链接:https://www.haomeiwen.com/subject/gokmqltx.html