日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python使用pngquant实现批量压缩图片

發布時間:2023/12/8 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python使用pngquant实现批量压缩图片 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

自己寫的一個批量壓縮圖片的小工具,文件結構如下圖

使用方法:將文件夾拖到bat.bat上,即可實現對文件夾內的圖片壓縮。

.bat文件中的代碼:

@echo off set floder=%1% set p_path=%~dp0 python %p_path%\reducePng.py -p %floder% pause

python代碼實現如下:

#!/usr/bin/python # -*- coding:UTF-8 -*-import os,os.path import sys,getopt import shutil#壓縮圖片 def reducePic(srcFile,dstFile):cmd=g_curDir+'\\pngquant.exe --force --verbose --speed=1 --ordered 256 %s --output %s' %(srcFile, dstFile)os.system(cmd)#循環遞歸遍歷文件夾 def traverse(file_dir):fs = os.listdir(file_dir)for dir in fs:tmp_path = os.path.join(file_dir, dir)if not os.path.isdir(tmp_path):tu=os.path.splitext(tmp_path)print(tmp_path)if tu[1] in g_reduceFileExt:newPath=tmp_path.replace(g_srcPath,g_dstPath)print(newPath)reducePic(tmp_path,newPath)else:createFloder(tmp_path.replace(g_srcPath,g_dstPath))traverse(tmp_path)def getFloderPath():opts,args=getopt.getopt(sys.argv[1:],"p:s:")file_path=""for op,value in opts:if op == "-p":file_path=valuereturn file_pathdef createFloder(dstpath):if not os.path.exists(dstpath): os.mkdir(dstpath) def main():#當前路徑global g_curDirg_curDir=os.path.dirname(os.path.realpath(__file__))#需要壓縮的圖片擴展名global g_reduceFileExtg_reduceFileExt=['.png','.jpg']#壓縮后的圖片存儲目錄global g_dstPathg_dstPath=g_curDir+'\\reduces'createFloder(g_dstPath)global g_srcPathg_srcPath=getFloderPath()print(g_srcPath)traverse(g_srcPath)print('files reduce success')if __name__ == '__main__':main()

附:

pngquant工具官網:https://pngquant.org/

參數詳情如下圖:

總結

以上是生活随笔為你收集整理的Python使用pngquant实现批量压缩图片的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。