丹桂飘香、秋风萧瑟、黄叶遍地。当头顶掠过个性的风,我知道,十一月来了。虽然红枫妖娆,野草还在坚持,可,白杨还是降下了翠绿的旗帜。一行雁鸣慢慢合拢台幕,盛宴即将结束。
本文为大家分享了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实现批量压缩图片请关注haodaima.com其它相关文章!