python批量压缩图片
2018-06-20 15:57
232 查看
from PIL import Image import os #图片压缩批处理 def compressImage(srcPath,dstPath): for filename in os.listdir(srcPath): #如果不存在目的目录则创建一个,保持层级结构 if not os.path.exists(dstPath): os.makedirs(dstPath) #拼接完整的文件或文件夹路径 srcFile=os.path.join(srcPath,filename) dstFile=os.path.join(dstPath,filename) print(srcFile) print(dstFile) #如果是文件就处理 if os.path.isfile(srcFile): #打开原图片缩小后保存,可以用if srcFile.endswith(".jpg")或者split,splitext等函数等针对特定文件压缩 sImg=Image.open(srcFile) w,h=sImg.size # print w,h dImg=sImg.resize((250,250),Image.ANTIALIAS) #设置压缩尺寸和选项,注意尺寸要用括号 dImg.save(dstFile) #也可以用srcFile原路径保存,或者更改后缀保存,save这个函数后面可以加压缩编码选项JPEG之类的 print(dstFile+" compressed succeeded") #如果是文件夹就递归 if os.path.isdir(srcFile): compressImage(srcFile,dstFile) if __name__=='__main__': path="D:\projects\ADC_new\Classify_0\source" path1 = "D:\projects\ADC_new\Classify_0\source1" compressImage(path,path1)阅读更多
相关文章推荐
- python 批量压缩图片
- 如何用Python智能批量压缩图片?
- Python简易图片批量压缩程序
- Python批量压缩图片
- Python实现批量压缩图片
- python实现图片批量压缩程序
- python脚本编程:批量压缩图片大小
- PYthon 批量下载网页图片
- shell 批量压缩指定目录及子目录内图片的方法
- Python 批量压缩png 支持过滤个别文件和文件夹
- 批量压缩ZIP中所有图片或改格式小工具
- 图片批量压缩大小脚本分享
- python图片批量裁剪
- Python如何批量复制图片到另一个文件夹中
- python实现批量修改图片格式和尺寸
- 批量图片压缩工具:JPGCompact 2.0绿色版
- Python 图形处理库PIL批量修改图片大小和格式
- Python批量修改图片分辨率
- java 图片 批量 压缩 +全部压缩
- -个图片质量批量压缩linux脚本