使用TinyPNG批量压缩图片

注册apikey

https://tinypng.com/developers

安装环境

需要用到python,pip

1、安装python

Python 2 需要大于2.7.9

 brew install python

或者 Python 3: 需要大于等于3.4

brew install python3

2、安装pip
使用pip官网get-pip.py安装一直不成功,通过下面命令可以

sudo easy_install pip 

3、安装tinify

sudo pip install --upgrade tinify

编写python脚本 tinypng.py

import tinify
import os
import os.path
import sys
print "name:", sys.argv[0]
tinify.key ="xxxxx" # AppKey
if len(sys.argv) < 3:
    print "params length is error"
    sys.exit()
else:
    fromPath = sys.argv[1] # source path
    toPath = sys.argv[2] # dest path
for root, dirs, files in os.walk(fromPath):
    newToPath = toPath
    if len(root) > len(fromPath):
        innerPath= root[len(fromPath):]
        if innerPath[0] == '/':
            innerPath = innerPath[1:]
        newToPath =  os.path.join(toPath,innerPath)
for name in files:
    newFromFilePath = os.path.join(root, name)
    newToFilePath = os.path.join(newToPath, name)
    fileName, fileSuffix = os.path.splitext(name)
    if fileSuffix == '.png' or fileSuffix == '.jpg':
        source = tinify.from_file(newFromFilePath)
        source.to_file(newToFilePath)
        print "compression successful : ", name
    else:
        pass
for dirName in dirs:
    os.mkdir(os.path.join(newToPath, dirName))

使用

python tinypng.py  frompath  topath
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值