Python实现批量压缩图片
2018-01-25 14:26
901 查看
本文为大家分享了Python实现批量压缩图片的具体代码,供大家参考,具体内容如下
# -*- coding: utf-8 -*- """ __author__= 'Du' __creation_time__= '2018/1/5 10:06' """ import os from PIL import Image import glob DIR = 'C:/Users/Public/Pictures/Sample Pictures/' class Compress_Picture(object): def __init__(self): # 图片格式,可以换成.bpm等 self.file = '.jpg' # 图片压缩批处理 def compressImage(self): for filename in glob.glob('%s%s%s' % (DIR, '*', self.file)): # print(filename) # 打开原图片压缩 sImg = Image.open(filename) w, h = sImg.size print(w, h) dImg = sImg.resize((200, 200), Image.ANTIALIAS) # 设置压缩尺寸和选项,注意尺寸要用括号 # 如果不存在目的目录则创建一个 comdic = "%scompress/"%DIR if not os.path.exists(comdic): os.makedirs(comdic) # 压缩图片路径名称 f1 = filename.split('/') f1 = f1[-1].split('\\') f2 = f1[-1].split('.') f2 = '%s%s1%s'%(comdic, f2[0], self.file) # print(f2) dImg.save(f2) # save这个函数后面可以加压缩编码选项JPEG之类的 print("%s compressed succeeded"%f1[-1]) if __name__ == "__main__": obj = Compress_Picture() obj.compressImage()
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- python实现图片批量压缩程序
- 使用python脚本实现iOS图片资源压缩
- php实现批量压缩图片文件大小的脚本
- Optipng,jpegoptim应用,用python实现图片压缩,让你的网站变得更快
- python 对图片或者文本实现批量重命名
- python 批量压缩图片
- Python批量压缩图片
- python实现图片批量剪切示例
- Python简易图片批量压缩程序
- python实现批量图片的抓取
- python实现批量下载贴吧图片
- 如何用Python智能批量压缩图片?
- Python实现的图片批量下载(v3.5最新)
- Python实现批量读取图片并存入mongodb数据库的方法示例
- python脚本编程:批量压缩图片大小
- python实现批量修改图片格式和尺寸
- python实现批量按比例缩放图片效果
- php实现批量压缩图片文件大小的脚本
- python通过opencv实现批量剪切图片
- Python实现简单爬虫功能--批量下载百度贴吧里的图片