Skip to content

Instantly share code, notes, and snippets.

@jerivas
Last active June 16, 2018 04:10
Show Gist options
  • Save jerivas/3085ba151c3e2f8e663d to your computer and use it in GitHub Desktop.
Save jerivas/3085ba151c3e2f8e663d to your computer and use it in GitHub Desktop.
TinyPNG API via Python. Optimizes .png and .jpg files in the current directory and outputs to "optimized/".
from __future__ import print_function
import os
import tinify
tinify.key = "" # Fill your developer key here
exts = ("jpg", "png", "jpeg")
basewidth = 1500
files = [f for f in os.listdir(".") if os.path.isfile(f) and f.endswith(exts)]
for f in files:
print("Optimizing %s" % f)
source = tinify.from_file(f)
resized = source.resize(method="scale", width=basewidth)
resized.to_file("optimized/%s" % f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment