Python之如何調(diào)整圖片的文件大小
問題描述
Python調(diào)整圖片文件的占用空間大小,而不是分辨率
1.jpg
圖片大小為 8KB
減小文件大小
使用 PIL
模塊
pip install Pillow
1. 減小圖片質(zhì)量
代碼
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): # 壓縮質(zhì)量遞減 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
效果
注意!無法實現(xiàn)圖片無限壓縮,若文件太小,辨識度也會大大降低
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
模塊調(diào)用系統(tǒng)命令 fsutil file createnew filename filesize
創(chuàng)建指定大小的文件
再用 copy/b
命令合并數(shù)據(jù)到圖片上
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]) # 創(chuàng)建臨時文件 subprocess.run(['copy/b', '{}/b+{}/b'.format(imagefile, tempfile), targetfile], shell=True) # 合并生成新圖片 os.remove(tempfile)
Linux
通過 subprocess
模塊調(diào)用系統(tǒng)命令 fallocate -l filesize filename
創(chuàng)建指定大小的文件
再用 cat >
命令合并數(shù)據(jù)到圖片上
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]) # 創(chuàng)建臨時文件 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): """調(diào)整圖片文件大小 :param imagefile: 原圖路徑 :param targetfile: 保存圖片路徑 :param targetsize: 目標文件大小,單位byte """ currentsize = os.path.getsize(imagefile) # 原圖文件大小 if currentsize > targetsize: # 需要縮小 for quality in range(99, 0, -1): # 壓縮質(zhì)量遞減 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]) # 創(chuàng)建臨時文件 subprocess.run(['copy/b', '{}/b+{}/b'.format(imagefile, tempfile), targetfile], shell=True) # 合并生成新圖片 elif system == 'Linux': subprocess.run(['fallocate', '-l', tempsize, tempfile]) # 創(chuàng)建臨時文件 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
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python模塊對Redis數(shù)據(jù)庫的連接與使用講解
這篇文章主要介紹了Python模塊對Redis數(shù)據(jù)庫的連接與使用,通過實例代碼給大家介紹了Python連接Redis數(shù)據(jù)庫方法,Python使用連接池連接Redis數(shù)據(jù)庫方法,感興趣的朋友跟隨小編一起看看吧2021-07-07使用Python編寫一個簡單的tic-tac-toe游戲的教程
這篇文章主要介紹了使用Python編寫一個簡單的tic-tac-toe游戲的教程,有利于Python初學(xué)者進行上手實踐,需要的朋友可以參考下2015-04-04