国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

python 批量壓縮圖片的腳本

瀏覽:8日期:2022-06-17 17:34:12
目錄簡介需要 Needs用法 Usage代碼實現(xiàn)效果另外一種圖片壓縮實現(xiàn)方式簡介

用Python批量壓縮圖片,把文件夾或圖片直接拖入即可

需要 Needs

Python 3

Pillow (用pip install pillow來安裝即可)

用法 Usage

把文件夾或圖片直接拖入即可。如果拖入的是文件夾,則會遍歷子文件夾把所有圖片都壓縮了。

注意,壓縮后的文件會直接替換原來的文件,文件名不變,尺寸不變,只改變壓縮質(zhì)量。

文件的開頭有兩個變量:

SIZE_CUT = 4 表示大于4MB的圖片都會進行壓縮

QUALITY = 90 表示壓縮質(zhì)量90,這個質(zhì)量基本人眼是看不出來啥差距的,而且很多原先10M的圖能壓縮一半。80以下的質(zhì)量大概就不太行了。

代碼

#!/usr/bin/python3# -*- coding: UTF-8 -*-# Created by Mario Chen, 01.04.2021, Shenzhen# My Github site: https://github.com/Mario-Heroimport sysimport osfrom PIL import ImageSIZE_CUT = 4 # picture over this size should be compressed. Units: MBQUALITY = 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:returnif __name__ == ’__main__’: for folder in sys.argv:#print(folder)compress(folder) print('Finish.') #os.system('pause')實現(xiàn)效果

python 批量壓縮圖片的腳本

壓縮后大小

python 批量壓縮圖片的腳本

另外一種圖片壓縮實現(xiàn)方式

同樣自動遍歷目錄下的圖片

import osfrom PIL import Imageimport threading,timedef imgToProgressive(path): if not path.split(’.’)[-1:][0] in [’png’,’jpg’,’jpeg’]: #if path isn’t a image file,returnreturn 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],’開始轉(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(’開始重命名文件’) 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 批量壓縮圖片的腳本的詳細內(nèi)容,更多關(guān)于python 批量壓縮圖片的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 国产成人久久精品二区三区 | 日韩精品在线观看免费 | 成人区精品一区二区毛片不卡 | 男女很舒服爽视频免费 | 国产亚洲一欧美一区二区三区 | 国产精品jvid在线观看 | 色多多最新地址福利地址 | 白嫩美女一级毛片免费看 | 亚洲人成一区二区三区 | 久久综合婷婷 | 国产专区中文字幕 | 丝袜美腿在线不卡视频播放 | 国产一区二区三区免费看 | 国产精品欧美激情在线播放 | 99久久精品国产片 | 免费在线观看黄色毛片 | 亚洲最大福利视频 | 国产成人黄网址在线视频 | 国产成人精品午夜在线播放 | 美女脱了内裤张开腿让男人桶网站 | 在线观看一级毛片免费 | 又黄又湿又爽吸乳视频 | 欧美另类69xxxxx极品 | 免费看片aⅴ免费大片 | 欧美日韩日本国产 | 中文字幕精品视频在线 | 女初高中福利视频在线观看 | 免费国产a | 怡红院免费全部视频在线视频 | 80日本xxxxxxxxx | 男女男精品视频免费观看 | 成人国产三级在线播放 | 91色久| 久久久久久久久国产 | 国内成人自拍 | 艳女伦交一级毛片 | 我们2018在线完整免费观看 | 综合自拍亚洲综合图区美腿丝袜 | 黄色三级网站在线观看 | 视频精品一区二区 | 亚洲欧美日本人成在线观看 |