参考链接 https://tinypng.com/developers/reference/python
1、安装
pip install --upgrade tinify
2、使用python脚本压缩图片
import tinify
tinify.key = "YOUR_API_KEY"
source = tinify.from_file("unoptimized.jpg")
source.to_file("optimized.jpg")
3、注册获取key
https://tinypng.com/developers 每个月有500张免费使用
4、例如:
import tinify
import os
import sys
# user lipeilong@joyodream.com rigister key
tinify.key = "Ly9VUp8wM1v9jBdThRgvyfRhll0QeW7X"
if len(sys.argv) >= :
srcPath = sys.argv[]
dstPath = sys.argv[]
else:
srcPath = "./src"
dstPath = "./dst" for (path, dirs, files) in os.walk(srcPath):
for filename in files:
srcName = srcPath + "/" + filename
dstName = dstPath + "/" + filename
source = tinify.from_file(srcName)
source.to_file(dstName)