亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁技術文章
文章詳情頁

python 批量壓縮圖片的腳本

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

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

需要 Needs

Python 3

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

用法 Usage

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

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

文件的開頭有兩個變量:

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

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

代碼

#!/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')實現效果

python 批量壓縮圖片的腳本

壓縮后大小

python 批量壓縮圖片的腳本

另外一種圖片壓縮實現方式

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

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],’開始轉換圖片’)img.save(destination, 'JPEG', quality=80, optimize=True, progressive=True) #轉換就是直接另存為print(path.split(’’)[-1:][0],’轉換完畢’) 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],’轉換完畢’) 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 批量壓縮圖片的腳本的詳細內容,更多關于python 批量壓縮圖片的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 国产精品欧美亚洲韩国日本 | 极品色在线精品视频 | 性欧美f | 草草免费视频 | 可以免费观看欧美一级毛片 | 超清波多野结衣精品一区 | 制服丝袜怡红院 | 一级久久 | 欧美精品午夜久久久伊人 | 欧美精品一区二区在线观看播放 | 97成人在线视频 | 亚洲国产精品aaa一区 | 国产精品亚欧美一区二区三区 | 国产一级一级一级成人毛片 | 国产成人免费高清视频 | 色综合色狠狠天天久久婷婷基地 | 自拍视频在线观看 | 欧美亚洲另类视频 | 亚洲午夜色 | 成人性动漫高清免费观看网址 | 日本免费人做人一区在线观看 | 5x社区直接进入一区二区三区 | 特级黄色毛片视频 | 日韩精品免费一区二区 | 久久国产一区二区三区 | 日本三级全黄三级a | 色伊人国产高清在线 | 普通话对白国产精品一级毛片 | 古代级a毛片可以免费看 | 精品伊人久久久久7777人 | 中文字幕一级毛片 | 欧美在线视频一区 | 久久综合一区二区三区 | 日本精品在线观看 | 正在播放国产大学生情侣 | 国产高清视频在线播放 | 欧美亚洲在线观看 | 中国美女隐私无遮挡免费视频 | 久久99国产精品久久99果冻传媒 | 久草手机视频在线观看 | 一区二区三区久久精品 |