Skip to content

Instantly share code, notes, and snippets.

@border
Last active May 11, 2016 07:21
Show Gist options
  • Save border/7a17342a5b05fd251aa7 to your computer and use it in GitHub Desktop.
Save border/7a17342a5b05fd251aa7 to your computer and use it in GitHub Desktop.

Revisions

  1. border revised this gist Aug 29, 2015. No changes.
  2. border revised this gist Aug 29, 2015. No changes.
  3. border created this gist Aug 29, 2015.
    53 changes: 53 additions & 0 deletions tinypng.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    #!/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)