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) + ' %') |