python 批量壓縮圖片的腳本
簡(jiǎn)介
用Python批量壓縮圖片,把文件夾或圖片直接拖入即可
需要 Needs
Python 3
Pillow (用pip install pillow來(lái)安裝即可)
用法 Usage
把文件夾或圖片直接拖入即可。如果拖入的是文件夾,則會(huì)遍歷子文件夾把所有圖片都?jí)嚎s了。
注意,壓縮后的文件會(huì)直接替換原來(lái)的文件,文件名不變,尺寸不變,只改變壓縮質(zhì)量。
文件的開(kāi)頭有兩個(gè)變量:
SIZE_CUT = 4 表示大于4MB的圖片都會(huì)進(jìn)行壓縮
QUALITY = 90 表示壓縮質(zhì)量90,這個(gè)質(zhì)量基本人眼是看不出來(lái)啥差距的,而且很多原先10M的圖能壓縮一半。80以下的質(zhì)量大概就不太行了。
代碼
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
# Created by Mario Chen, 01.04.2021, Shenzhen
# My Github site: https://github.com/Mario-Hero
import sys
import os
from PIL import Image
SIZE_CUT = 4 # picture over this size should be compressed. Units: MB
QUALITY = 90 # 90 is good, this number should not be smaller than 80.
def isPic(name):
namelower = name.lower()
return namelower.endswith("jpeg") or namelower.endswith("jpg") or namelower.endswith("png")
def compressImg(file):
#print("The size of", file, "is: ", os.path.getsize(file))
im = Image.open(file)
im.save(file, quality=QUALITY)
def compress(folder):
try:
if os.path.isdir(folder):
print(folder)
file_list = os.listdir(folder)
for file in file_list:
if os.path.isdir(folder+"/"+file):
#print(folder +"/"+ file)
compress(folder +"/"+file)
else:
if isPic(file):
if os.path.getsize(folder + "/" + file) > (SIZE_CUT * 1024 * 1024):
compressImg(folder + "/" + file)
print(file)
else:
if isPic(folder):
if os.path.getsize(folder) > (SIZE_CUT * 1024 * 1024):
compressImg(folder)
except BaseException:
return
if __name__ == '__main__':
for folder in sys.argv:
#print(folder)
compress(folder)
print("Finish.")
#os.system("pause")
實(shí)現(xiàn)效果

壓縮后大小

另外一種圖片壓縮實(shí)現(xiàn)方式
同樣自動(dòng)遍歷目錄下的圖片
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 批量壓縮圖片的腳本的詳細(xì)內(nèi)容,更多關(guān)于python 批量壓縮圖片的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python sqlparse 解析庫(kù)的基礎(chǔ)使用過(guò)程解析
sqlparse 是一個(gè) Python 庫(kù),是一個(gè)用于 Python 的非驗(yàn)證 SQL 解析器, 用于解析 SQL 語(yǔ)句并提供一個(gè)簡(jiǎn)單的 API 來(lái)訪問(wèn)解析后的 SQL 結(jié)構(gòu),這篇文章主要介紹了Python sqlparse 解析庫(kù)的基礎(chǔ)使用,需要的朋友可以參考下2024-08-08
如何利用Python連接MySQL數(shù)據(jù)庫(kù)實(shí)現(xiàn)數(shù)據(jù)儲(chǔ)存
當(dāng)我們學(xué)習(xí)了mysql數(shù)據(jù)庫(kù)后,我們會(huì)想著該如何將python和mysql結(jié)合起來(lái)運(yùn)用,下面這篇文章主要給大家介紹了關(guān)于如何利用Python連接MySQL數(shù)據(jù)庫(kù)實(shí)現(xiàn)數(shù)據(jù)儲(chǔ)存的相關(guān)資料,需要的朋友可以參考下2021-11-11
python找出列表中大于某個(gè)閾值的數(shù)據(jù)段示例
今天小編就為大家分享一篇python找出列表中大于某個(gè)閾值的數(shù)據(jù)段示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
PyQt4 treewidget 選擇改變顏色,并設(shè)置可編輯的方法
今天小編就為大家分享一篇PyQt4 treewidget 選擇改變顏色,并設(shè)置可編輯的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
Python 啟動(dòng)時(shí)選擇32位 或64位版的操作
這篇文章主要介紹了Python 啟動(dòng)時(shí)選擇32位 或64位版的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
Python設(shè)計(jì)模式之橋接模式原理與用法實(shí)例分析
這篇文章主要介紹了Python設(shè)計(jì)模式之橋接模式原理與用法,結(jié)合具體實(shí)例形式分析了Python橋接模式的相關(guān)概念、原理、定義及使用方法,需要的朋友可以參考下2019-01-01

