python實(shí)現(xiàn)圖片批量壓縮
項(xiàng)目中大量用到圖片加載,由于圖片太大,加載速度很慢,因此需要對(duì)文件進(jìn)行統(tǒng)一壓縮
第一種
一:安裝包
python -m pip install Pillow
二:導(dǎo)入包
from PIL import Image import os
三:獲取圖片文件的大小
def get_size(file):
# 獲取文件大小:KB
size = os.path.getsize(file)
return size / 1024
四:輸出文件夾下的文件
dir_path = r'file_path'
items = os.listdir(dir_path)
for item in items:
# print(item)
path = os.path.join(dir_path, item)
print(item)
五:壓縮文件到指定大小,我期望的是150KB,step和quality可以修改到最合適的數(shù)值
def compress_image(infile, outfile=None, mb=150, step=10, quality=80):
"""不改變圖片尺寸壓縮到指定大小
:param infile: 壓縮源文件
:param outfile: 壓縮文件保存地址
:param mb: 壓縮目標(biāo),KB
:param step: 每次調(diào)整的壓縮比率
:param quality: 初始?jí)嚎s比率
:return: 壓縮文件地址,壓縮文件大小
"""
if outfile is None:
outfile = infile
o_size = get_size(infile)
if o_size <= mb:
im = Image.open(infile)
im.save(outfile)
while o_size > mb:
im = Image.open(infile)
im.save(outfile, quality=quality)
if quality - step < 0:
break
quality -= step
o_size = get_size(outfile)
六:修改圖片尺寸,如果同時(shí)有修改尺寸和大小的需要,可以先修改尺寸,再壓縮大小
def resize_image(infile, outfile='', x_s=800):
"""修改圖片尺寸
:param infile: 圖片源文件
:param outfile: 重設(shè)尺寸文件保存地址
:param x_s: 設(shè)置的寬度
:return:
"""
im = Image.open(infile)
x, y = im.size
y_s = int(y * x_s / x)
out = im.resize((x_s, y_s), Image.ANTIALIAS)
out.save(outfile)
七:運(yùn)行程序
if __name__ == '__main__':
# 源路徑 # 壓縮后路徑
compress_image(r"file_path", r"E:\docs\2.JPG")
# 源路徑 # 壓縮后路徑
resize_image(r"file_path", r"E:\docs\3.JPG")
第二種
import os
from PIL import Image
import threading,time
def imgToProgressive(path):
if not path.split('.')[-1:][0] in ['png','jpg','jpeg']: #if path isn't a image file,return
return
if os.path.isdir(path):
return
##########transform img to progressive
img = Image.open(path)
destination = path.split('.')[:-1][0]+'_destination.'+path.split('.')[-1:][0]
try:
print(path.split('\\')[-1:][0],'開(kāi)始轉(zhuǎn)換圖片')
img.save(destination, "JPEG", quality=80, optimize=True, progressive=True) #轉(zhuǎn)換就是直接另存為
print(path.split('\\')[-1:][0],'轉(zhuǎn)換完畢')
except IOError:
PIL.ImageFile.MAXBLOCK = img.size[0] * img.size[1]
img.save(destination, "JPEG", quality=80, optimize=True, progressive=True)
print(path.split('\\')[-1:][0],'轉(zhuǎn)換完畢')
print('開(kāi)始重命名文件')
os.remove(path)
os.rename(destination,path)
for d,_,fl in os.walk(os.getcwd()): #遍歷目錄下所有文件
for f in fl:
try:
imgToProgressive(d+'\\'+f)
except:
pass
以上就是python實(shí)現(xiàn)圖片批量壓縮的詳細(xì)內(nèi)容,更多關(guān)于python 圖片壓縮的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
pytorch繪制并顯示loss曲線和acc曲線,LeNet5識(shí)別圖像準(zhǔn)確率
今天小編就為大家分享一篇pytorch繪制并顯示loss曲線和acc曲線,LeNet5識(shí)別圖像準(zhǔn)確率,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
如何利用Python給自己的頭像加一個(gè)小國(guó)旗(小月餅)
這篇文章主要給大家介紹了關(guān)于如何利用Python給自己的頭像加一個(gè)小國(guó)旗(小月餅)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Python?中給請(qǐng)求設(shè)置用戶(hù)代理?User-Agent的方法
本文介紹?HTTP?標(biāo)頭用戶(hù)代理主題以及如何使用?Python?中的請(qǐng)求設(shè)置用戶(hù)代理,您將了解?HTTP?標(biāo)頭及其在理解用戶(hù)代理、獲取用戶(hù)代理以及學(xué)習(xí)使用?Python?中的請(qǐng)求設(shè)置用戶(hù)代理的多種方法方面的重要性,感興趣的朋友跟隨小編一起看看吧2023-06-06
python3實(shí)現(xiàn)繪制二維點(diǎn)圖
今天小編就為大家分享一篇python3實(shí)現(xiàn)繪制二維點(diǎn)圖,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
Python Selenium中等待設(shè)置的實(shí)現(xiàn)
本文主要介紹了Python Selenium中等待設(shè)置的實(shí)現(xiàn),過(guò)詳實(shí)的示例代碼,深入介紹了顯式等待、隱式等待、自定義等待條件、多重等待條件、頁(yè)面加載狀態(tài)的等待、元素存在與可見(jiàn)性等待、Fluent等待以及異步JavaScript加載的等待,感興趣的可以了解一下2023-12-12

