Last active
May 11, 2016 07:21
-
-
Save border/7a17342a5b05fd251aa7 to your computer and use it in GitHub Desktop.
The Tinify API allows you to compress and optimize JPEG and PNG images. Base on https://tinypng.com/developers/reference/python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import sys | |
import os | |
import getopt | |
import tinify | |
tinify.key = 'YOUR_API_KEY' | |
def usage(): | |
print "python " + sys.argv[0] + " -i inputfile" | |
def tinypng(source): | |
filepath = os.path.dirname(source) | |
filename = os.path.basename(source) | |
target = filepath + os.sep + "tiny-" + filename | |
print target | |
tinify.from_file(source).to_file(target) | |
def scandir(startdir): | |
os.chdir(startdir) | |
for obj in os.listdir(os.curdir): | |
if os.path.isdir(obj): | |
scandir(obj) | |
os.chdir(os.pardir) | |
else: | |
filename = os.getcwd() + os.sep + obj | |
fname=os.path.splitext(filename) | |
if fname[1].lower() == ".png": | |
tinypng(filename) | |
opts, args = getopt.getopt(sys.argv[1:], "(hH)i:", ["help", "input="]) | |
input_file = '' | |
for op, value in opts: | |
if op == "-i": | |
input_file = value | |
elif op == "-h": | |
usage() | |
sys.exit() | |
if len(sys.argv) != 3: | |
usage() | |
sys.exit() | |
if os.path.isdir(input_file): | |
scandir(input_file) | |
else: | |
tinypng(input_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment