首页 > 编程知识 正文

python怎么设置片大小,python显示片大小如何改变

时间:2023-05-03 23:54:33 阅读:285030 作者:1788

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

问题描述

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

1.jpg

图片大小为 8KB




减小文件大小

使用 PIL 模块

pip install Pillow 1. 减小图片质量

代码

import osfrom PIL import Imagedef 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 osfrom PIL import Imagedef 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 osimport timeimport subprocessimagefile = '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 osimport timeimport subprocessimagefile = '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 osimport timeimport platformimport subprocessfrom PIL import Imagedef 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?

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。