Skip to content

Instantly share code, notes, and snippets.

@Yelakelly
Created April 22, 2016 22:38
Show Gist options
  • Save Yelakelly/4b3cdb65cb9899afba93b14f7df0887b to your computer and use it in GitHub Desktop.
Save Yelakelly/4b3cdb65cb9899afba93b14f7df0887b to your computer and use it in GitHub Desktop.
TinyPng - compression of all files in catalog

TinyPng

TinyPng - compression of all files in catalog.

All optimized images will go in "optimized" folder.

import sys
import os
from os import listdir
from os.path import isfile, join
import tinify
tinify.key = 'YOUR KEY'
IMAGES_FOLDER = sys.path[0]
FILE_EXTENTIONS = ['.png','.jpg']
all_images = []
if not os.path.exists('optimized_images'):
os.makedirs('optimized_images')
for f in listdir(IMAGES_FOLDER):
file_extention = os.path.splitext(f)[1]
if isfile(join(IMAGES_FOLDER, f)) and file_extention in FILE_EXTENTIONS:
all_images.append(f)
count = 0
for f in all_images:
source = tinify.from_file(f)
source.to_file("optimized_images\\" + f)
count+=1
print('Finished: ' + str(float(count) / float(len(all_images)) * 100) + ' %')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment