Python之如何调整图片的文件大小
作者:XerCis 时间:2022-09-27 04:57:50
问题描述
Python调整图片文件的占用空间大小,而不是分辨率
1.jpg
图片大小为 8KB
减小文件大小
使用 PIL
模块
pip install Pillow
1. 减小图片质量
代码
import os
from PIL import Image
def compress_under_size(imagefile, targetfile, targetsize):
"""压缩图片尺寸直到某一尺寸
:param imagefile: 原图路径
:param targetfile: 保存图片路径
:param targetsize: 目标大小,单位byte
"""
currentsize = os.path.getsize(imagefile)
for quality in range(99, 0, -1): # 压缩质量递减
if currentsize > targetsize:
image = Image.open(imagefile)
image.save(targetfile, optimize=True, quality=quality)
currentsize = os.path.getsize(targetfile)
if __name__ == '__main__':
imagefile = '1.jpg' # 图片路径
targetfile = 'result.jpg' # 目标图片路径
targetsize = 2 * 1024 # 目标图片大小
compress_under_size(imagefile, targetfile, targetsize) # 将图片压缩到2KB
效果
注意!无法实现图片无限压缩,若文件太小,辨识度也会大大降低
2. 减小图片尺寸
import os
from PIL import Image
def image_compress(filename, savename, targetsize):
"""图像压缩
:param filename: 原图路径
:param savename: 保存图片路径
:param targetsize: 目标大小,单位为byte
"""
image = Image.open(filename)
size = os.path.getsize(filename)
if size <= targetsize:
return
width, height = image.size
num = (targetsize / size) ** 0.5
width, height = round(width * num), round(height * num)
image.resize((width, height)).save(savename)
if __name__ == '__main__':
filename = '1.jpg'
savename = 'result.jpg'
targetsize = 2 * 1024
image_compress(filename, savename, targetsize)
效果
增加文件大小
Windows
通过 subprocess
模块调用系统命令 fsutil file createnew filename filesize
创建指定大小的文件
再用 copy/b
命令合并数据到图片上
import os
import time
import subprocess
imagefile = '1.jpg' # 图片路径
targetfile = 'result.jpg' # 目标图片路径
targetsize = 10 * 1024 * 1024 # 目标图片大小
tempfile = str(int(time.time())) # 临时文件路径
tempsize = str(targetsize - os.path.getsize(imagefile)) # 临时文件大小
subprocess.run(['fsutil', 'file', 'createnew', tempfile, tempsize]) # 创建临时文件
subprocess.run(['copy/b', '{}/b+{}/b'.format(imagefile, tempfile), targetfile], shell=True) # 合并生成新图片
os.remove(tempfile)
Linux
通过 subprocess
模块调用系统命令 fallocate -l filesize filename
创建指定大小的文件
再用 cat >
命令合并数据到图片上
import os
import time
import subprocess
imagefile = '1.jpg' # 图片路径
targetfile = 'result.jpg' # 目标图片路径
targetsize = 10 * 1024 * 1024 # 目标图片大小
tempfile = str(int(time.time())) # 临时文件路径
tempsize = str(targetsize - os.path.getsize(imagefile)) # 临时文件大小
subprocess.run(['fallocate', '-l', tempsize, tempfile]) # 创建临时文件
subprocess.run('cat {} {} > {}'.format(imagefile, tempfile, targetfile), shell=True) # 合并生成新图片
os.remove(tempfile)
效果
图片的分辨率没变
封装
import os
import time
import platform
import subprocess
from PIL import Image
def resize_picture_filesize(imagefile, targetfile, targetsize):
"""调整图片文件大小
:param imagefile: 原图路径
:param targetfile: 保存图片路径
:param targetsize: 目标文件大小,单位byte
"""
currentsize = os.path.getsize(imagefile) # 原图文件大小
if currentsize > targetsize: # 需要缩小
for quality in range(99, 0, -1): # 压缩质量递减
if currentsize > targetsize:
image = Image.open(imagefile)
image.save(targetfile, optimize=True, quality=quality)
currentsize = os.path.getsize(targetfile)
else: # 需要放大
system = platform.system()
tempfile = str(int(time.time())) # 临时文件路径
tempsize = str(targetsize - os.path.getsize(imagefile)) # 临时文件大小
if system == 'Windows':
subprocess.run(['fsutil', 'file', 'createnew', tempfile, tempsize]) # 创建临时文件
subprocess.run(['copy/b', '{}/b+{}/b'.format(imagefile, tempfile), targetfile], shell=True) # 合并生成新图片
elif system == 'Linux':
subprocess.run(['fallocate', '-l', tempsize, tempfile]) # 创建临时文件
subprocess.run('cat {} {} > {}'.format(imagefile, tempfile, targetfile), shell=True) # 合并生成新图片
os.remove(tempfile)
if __name__ == '__main__':
imagefile = '1.jpg' # 8KB的图片
resize_picture_filesize(imagefile, 'reduce.jpg', 2 * 1024) # 缩小到2KB
resize_picture_filesize(imagefile, 'increase.jpg', 800 * 1024) # 放大到800KB
来源:https://blog.csdn.net/lly1122334/article/details/118366401
标签:Python,图片,文件大小
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python面向对象魔法方法和单例模块代码实例
2023-05-02 19:03:16
Python简单网络编程示例【客户端与服务端】
2023-12-07 10:26:55
FrontPage XP设计教程5——表单的设计
2008-10-11 12:35:00
![](https://img.aspxhome.com/file/UploadPic/200810/17/2008101712380183s.jpg)
浅谈PHP的反射机制
2023-06-13 17:05:35
Python socket 套接字实现通信详解
2023-05-16 04:18:08
pytorch 利用lstm做mnist手写数字识别分类的实例
2023-01-31 03:15:38
学习ASP的理由 分析小结
2011-02-26 10:54:00
Python+PyQt5制作一个图片查看器
2021-03-03 04:16:06
![](https://img.aspxhome.com/file/2023/5/87995_0s.gif)
Python处理mysql数据库
2010-12-03 16:23:00
Django使用Profile扩展User模块方式
2023-06-16 12:26:12
![](https://img.aspxhome.com/file/2023/0/70090_0s.jpg)
MySql数据库基本命令集会
2011-08-05 18:43:23
asp.net性能的技巧
2007-10-07 21:55:00
Python接口测试文件上传实例解析
2023-04-19 15:59:28
![](https://img.aspxhome.com/file/2023/1/67311_0s.jpg)
MSSQL存储过程解秘过程全析
2010-07-05 08:49:00
python 基础学习第二弹 类属性和实例属性
2023-08-02 04:07:44
python清除字符串中间空格的实例讲解
2023-12-29 22:40:24
Python 匹配任意字符(包括换行符)的正则表达式写法
2023-01-23 23:11:09
Python实时监控网站浏览记录实现过程详解
2021-06-24 23:55:02
![](https://img.aspxhome.com/file/2023/8/97218_0s.jpg)
基于Python实现开心消消乐小游戏的示例代码
2022-03-20 13:19:10
![](https://img.aspxhome.com/file/2023/8/86428_0s.jpg)
Javascript 获取css属性
2009-05-31 16:49:00