Mac工作流之图片压缩

需要安装命令行插件
pngquant jpegoptim gifsicle

import sys
import subprocess as sp
import os
import json
import shutil

my_env = os.environ.copy()
my_env["PATH"] = "/usr/local/bin:" + my_env["PATH"]
# Note: The first argument is the script's path

for arg in sys.argv[1:]:
        fileType = (os.path.splitext(arg)[-1]).lower()
        if fileType == ".png":
                my_command = ["pngquant", arg, "--quality", "70-95", "--ext=.png", "--force"]
                sp.check_output(my_command, env=my_env)
        elif fileType == ".jpg" or fileType == ".jpeg":
                my_command = ["jpegoptim", "-m70", "--max90", arg] 
                sp.check_output(my_command, env=my_env)
        elif fileType == ".gif":
                my_command = ["gifsicle", "-i", arg, "--optimize=3", "-o", arg] 
                sp.check_output(my_command, env=my_env)
posted @ 2023-05-04 21:42  CoderWGB  阅读(51)  评论(0编辑  收藏  举报