Python调整图片的文件大小

您所在的位置:网站首页 python调整图片大小 Python调整图片的文件大小

Python调整图片的文件大小

2023-09-11 05:35| 来源: 网络整理| 查看: 265

文章目录 问题描述减小文件大小1. 减小图片质量2. 减小图片尺寸 增加文件大小封装参考文献

问题描述

Python调整图片文件的占用空间大小,而不是分辨率

1.jpg 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 命令合并数据到图片上

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

参考文献 Python批量直接修改图片存储大小脚本How do I install PythonMagick for Python 3.5PythonMagick GitHubImageMagick DocumentationDOS Command: COPYpython给png图片写入无用数据从而改变其md5Create a File of Specific Size in Windows 10How To Create Files Of A Certain Size In LinuxPython执行系统命令How can I copy several binary files into one file on a Linux system?How to compress a picture less than a limit file size using python PIL library?


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3