Python批量压缩图片
2018-01-05 11:20
591 查看
# -*- 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实现批量压缩图片
- python脚本编程:批量压缩图片大小
- Python简易图片批量压缩程序
- python 批量压缩图片
- python实现图片批量压缩程序
- Thumbnailator java图片压缩,加水印,批量生成缩略图
- 利用python进行批量导出时态图片
- Python Show-Me-the-Code 第 0022 题 批量图片处理2
- java 图片 批量 压缩 +全部压缩
- 用批处理批量压缩图片
- python网络爬虫系列(四) --- 批量抓取并保存图片
- python下载批量图片
- python3.6深度学习图片集处理项目源代码(为cnn等准备图片集合,批量生成、删除、改名、旋转、调整图片大小,包括删除指定路径下所有文件)
- python--批量下载豆瓣图片之升级版本
- python实现批量图片的抓取
- Python入门小练习 002 批量下载网页链接中的图片
- python批量修改图片后缀的方法(png到jpg)
- Python实现简单爬虫功能--批量下载百度贴吧里的图片
- python-批量添加图片水印