Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haibinpark/b95d9ffec00446b09fcfba5c914b8532 to your computer and use it in GitHub Desktop.
Save haibinpark/b95d9ffec00446b09fcfba5c914b8532 to your computer and use it in GitHub Desktop.
webp图片转jpg的python脚本
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import time
# 遍历指定目录,显示目录下的所有文件名
def convertWebp2JpgInDirectory(dir):
if os.path.isdir(dir):
allfiles = os.listdir(dir)
for fi in allfiles:
fi_d = os.path.join(dir, fi)
if os.path.isdir(fi_d):
convertWebp2JpgInDirectory(fi_d)
else:
if fi_d.endswith(".webp"):
webp = os.path.join(dir, fi_d)
filename = webp.split("/")[-1]
filename = filename.replace(".webp",".jpg")
filedir = "/".join(webp.split("/")[:-1])
filedir = "%s_jpg"%filedir
if not os.path.exists(filedir):
os.makedirs(filedir)
jpg = "%s/%s"%(filedir, filename)
print("%s" %jpg)
commandline = "dwebp %s -o %s" % (webp, jpg)
os.system(commandline)
print(webp + " ------> 转换成功")
if __name__ == "__main__":
convertWebp2JpgInDirectory("/Users/haibin/talor/")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment