python图片批量压缩有什么方法?
Admin 2021-08-18 群英技术资讯 1425 次浏览
python图片批量压缩有什么方法?我们在做项目的时候,可能会使用到图片,而图片太大,那么对于加载速度有一定的影响,因此对图片文件压缩是很有必要的,而一张张图片分别压缩是不太可能,对此,下面我们就来看看python如何实现图片批量压缩。
python -m pip install Pillow
from PIL import Image import os
def get_size(file):
# 获取文件大小:KB
size = os.path.getsize(file)
return size / 1024
dir_path = r'file_path'
items = os.listdir(dir_path)
for item in items:
# print(item)
path = os.path.join(dir_path, item)
print(item)
def compress_image(infile, outfile=None, mb=150, step=10, quality=80):
"""不改变图片尺寸压缩到指定大小
:param infile: 压缩源文件
:param outfile: 压缩文件保存地址
:param mb: 压缩目标,KB
:param step: 每次调整的压缩比率
:param quality: 初始压缩比率
:return: 压缩文件地址,压缩文件大小
"""
if outfile is None:
outfile = infile
o_size = get_size(infile)
if o_size <= mb:
im = Image.open(infile)
im.save(outfile)
while o_size > mb:
im = Image.open(infile)
im.save(outfile, quality=quality)
if quality - step < 0:
break
quality -= step
o_size = get_size(outfile)
def resize_image(infile, outfile='', x_s=800):
"""修改图片尺寸
:param infile: 图片源文件
:param outfile: 重设尺寸文件保存地址
:param x_s: 设置的宽度
:return:
"""
im = Image.open(infile)
x, y = im.size
y_s = int(y * x_s / x)
out = im.resize((x_s, y_s), Image.ANTIALIAS)
out.save(outfile)
if __name__ == '__main__':
# 源路径 # 压缩后路径
compress_image(r"file_path", r"E:\docs\2.JPG")
# 源路径 # 压缩后路径
resize_image(r"file_path", r"E:\docs\3.JPG")
import os
from PIL import Image
import threading,time
def imgToProgressive(path):
if not path.split('.')[-1:][0] in ['png','jpg','jpeg']: #if path isn't a image file,return
return
if os.path.isdir(path):
return
##########transform img to progressive
img = Image.open(path)
destination = path.split('.')[:-1][0]+'_destination.'+path.split('.')[-1:][0]
try:
print(path.split('\\')[-1:][0],'开始转换图片')
img.save(destination, "JPEG", quality=80, optimize=True, progressive=True) #转换就是直接另存为
print(path.split('\\')[-1:][0],'转换完毕')
except IOError:
PIL.ImageFile.MAXBLOCK = img.size[0] * img.size[1]
img.save(destination, "JPEG", quality=80, optimize=True, progressive=True)
print(path.split('\\')[-1:][0],'转换完毕')
print('开始重命名文件')
os.remove(path)
os.rename(destination,path)
for d,_,fl in os.walk(os.getcwd()): #遍历目录下所有文件
for f in fl:
try:
imgToProgressive(d+'\\'+f)
except:
pass
以上就是关于python图片批量压缩的方法介绍,上文介绍了两种图片批量压缩的方法,大家可以参考,希望文本对大家学习python有帮助,想要了解更多python图片批量压缩的内容,大家可以继续关注其他文章。
文本转载自脚本之家
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
本文介绍的pdf转word功能还有一些待完善地方,例如可增加预览功能,实现每页预览,当然我们可以在后续阶段逐渐完善,对基于PyQt5完成的pdf转word功能感兴趣的朋友一起看看吧
在pandas中,当经常对数据进行处理,可能会造成数据索引顺序混乱,那么也就会影响数据读取、插入等等操作,因此重置索引的操作就很重要,那么pandas中重置索引怎样做?接下来给大家分享几个方法,大家可以参考。
Splinter是一个使用Python测试Web应用程序的开源工具,可以自动化浏览器操作,使用Splinter可以使用pyhton脚本来实现,具体安装及操作方法跟随小编一起看看吧
这篇文章主要为大家介绍了如何利用Python实现将多张图片合成mp4视频,并加入背景音乐。文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
用python语言实现计算1~100的累加和(包含1和100),直接看代码
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008