PNG、 JPG图片压缩方法

参考链接 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
  2 import os
  3 import sys
  4 # user [email protected] rigister key
  5 tinify.key  = "Ly9VUp8wM1v9jBdThRgvyfRhll0QeW7X"
  6 if len(sys.argv) >= 3:
  7     srcPath     = sys.argv[1]
  8     dstPath     = sys.argv[2]
  9 else:
 10     srcPath     = "./src"
 11     dstPath     = "./dst"
 12 
 13 for (path, dirs, files) in os.walk(srcPath):
 14     for filename in files:
 15         srcName     = srcPath + "/" + filename
 16         dstName     = dstPath + "/" + filename
 17         source      = tinify.from_file(srcName)
 18         source.to_file(dstName)

 

你可能感兴趣的:(PNG、 JPG图片压缩方法)