python實現多文件夾下圖片類型轉換(png轉webp)

import glob import os import threading from PIL import Image def create_image(infile, index): os.path.splitext(infile) im = Image.open(infile) filePath = infile[0:infile.index("/")] fileName = infile[str(infile).find("/") + 1:str(infile).find('.png')] newFile = filePath + "/" + fileName + ".webp" try: im.save(newFile, "WEBP") except IOError: if os.path.exists(newFile + ".webp"): os.remove(newFile + ".webp") else: print(newFile + ".webp" + "..........ok!........") if os.path.exists(infile): os.remove(infile) print("........delete....." + infile + ".....ok") if os.path.exists(newFile) and os.path.exists(infile): os.remove(newFile); def start(): projectPath = "項目目錄/app/src/main/res/" # projectPath="" filePathArr = [projectPath + 'drawable', projectPath + 'drawable-mdpi', projectPath + 'drawable-ldpi', projectPath + 'drawable-hdpi', projectPath + 'drawable-xhdpi', projectPath + 'drawable-xxhdpi', projectPath + 'drawable-xhdpi'] for filePath in filePathArr: index = 0 for infile in glob.glob(filePath + "/*.png"): if infile.endswith(".9.png"): print(infile + ".....not do.....") else: t = threading.Thread(target=create_image, args=(infile, index)) t.start() t.join() index += 1 # 刪除文件,注意改上面的文件後綴名 # print(infile) # if os.path.exists(infile): # os.remove(infile) if __name__ == "__main__": start()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章