Python之如何調(diào)整圖片的文件大小
問(wèn)題描述
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: 目標(biāo)大小,單位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' # 目標(biāo)圖片路徑 targetsize = 2 * 1024 # 目標(biāo)圖片大小 compress_under_size(imagefile, targetfile, targetsize) # 將圖片壓縮到2KB
效果
注意!無(wú)法實(shí)現(xiàn)圖片無(wú)限壓縮,若文件太小,辨識(shí)度也會(huì)大大降低
2. 減小圖片尺寸
import os from PIL import Image def image_compress(filename, savename, targetsize): """圖像壓縮 :param filename: 原圖路徑 :param savename: 保存圖片路徑 :param targetsize: 目標(biāo)大小,單位為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
通過(guò) 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' # 目標(biāo)圖片路徑 targetsize = 10 * 1024 * 1024 # 目標(biāo)圖片大小 tempfile = str(int(time.time())) # 臨時(shí)文件路徑 tempsize = str(targetsize - os.path.getsize(imagefile)) # 臨時(shí)文件大小 subprocess.run(['fsutil', 'file', 'createnew', tempfile, tempsize]) # 創(chuàng)建臨時(shí)文件 subprocess.run(['copy/b', '{}/b+{}/b'.format(imagefile, tempfile), targetfile], shell=True) # 合并生成新圖片 os.remove(tempfile)
Linux
通過(guò) 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' # 目標(biāo)圖片路徑 targetsize = 10 * 1024 * 1024 # 目標(biāo)圖片大小 tempfile = str(int(time.time())) # 臨時(shí)文件路徑 tempsize = str(targetsize - os.path.getsize(imagefile)) # 臨時(shí)文件大小 subprocess.run(['fallocate', '-l', tempsize, tempfile]) # 創(chuàng)建臨時(shí)文件 subprocess.run('cat {} {} > {}'.format(imagefile, tempfile, targetfile), shell=True) # 合并生成新圖片 os.remove(tempfile)
效果
圖片的分辨率沒(méi)變
封裝
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: 目標(biāo)文件大小,單位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())) # 臨時(shí)文件路徑 tempsize = str(targetsize - os.path.getsize(imagefile)) # 臨時(shí)文件大小 if system == 'Windows': subprocess.run(['fsutil', 'file', 'createnew', tempfile, tempsize]) # 創(chuàng)建臨時(shí)文件 subprocess.run(['copy/b', '{}/b+{}/b'.format(imagefile, tempfile), targetfile], shell=True) # 合并生成新圖片 elif system == 'Linux': subprocess.run(['fallocate', '-l', tempsize, tempfile]) # 創(chuàng)建臨時(shí)文件 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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
輕松掌握python設(shè)計(jì)模式之訪問(wèn)者模式
這篇文章主要幫助大家輕松掌握python設(shè)計(jì)模式之訪問(wèn)者模式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Python之PyQt6對(duì)話框的實(shí)現(xiàn)
這篇文章主要介紹了Python之PyQt6對(duì)話框的實(shí)現(xiàn),文章內(nèi)容詳細(xì),簡(jiǎn)單易懂,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2023-01-01Python模塊對(duì)Redis數(shù)據(jù)庫(kù)的連接與使用講解
這篇文章主要介紹了Python模塊對(duì)Redis數(shù)據(jù)庫(kù)的連接與使用,通過(guò)實(shí)例代碼給大家介紹了Python連接Redis數(shù)據(jù)庫(kù)方法,Python使用連接池連接Redis數(shù)據(jù)庫(kù)方法,感興趣的朋友跟隨小編一起看看吧2021-07-07使用Python編寫一個(gè)簡(jiǎn)單的tic-tac-toe游戲的教程
這篇文章主要介紹了使用Python編寫一個(gè)簡(jiǎn)單的tic-tac-toe游戲的教程,有利于Python初學(xué)者進(jìn)行上手實(shí)踐,需要的朋友可以參考下2015-04-04