Last active
May 19, 2016 06:57
-
-
Save karlcow/3541bcfbc87cc8327da8c693a3e7d1cc to your computer and use it in GitHub Desktop.
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
# Testing Images optimization | |
# for https://github.com/webcompat/webcompat.com/issues/1051 | |
import os | |
import shutil | |
import time | |
from PIL import Image | |
IMAGE_PATH = '/Users/karl/Documents/2016/05/webc/original.png' | |
IMAGE_DEST = '/Users/karl/Documents/2016/05/webc/saved' | |
JPEG_PATH = '/Users/karl/Documents/2016/05/webc/burger.jpg' | |
# Cleaning the directory | |
if os.path.exists(IMAGE_DEST): | |
shutil.rmtree(IMAGE_DEST) | |
os.makedirs(IMAGE_DEST) | |
else: | |
os.makedirs(IMAGE_DEST) | |
# We will be reading each time the file to be sure to start fresh. | |
def optim_true(IMAGE_PATH, IMAGE_DEST): | |
time_beg = time.time() | |
save_parameters = {} | |
image_file = '%s/%s' % (IMAGE_DEST, 'opt-true.png') | |
image_object = Image.open(IMAGE_PATH) | |
save_parameters['optimize'] = True | |
image_object.save(image_file, **save_parameters) | |
print image_object.encoderconfig | |
print image_object.encoderinfo | |
del image_object | |
time_end = time.time() | |
print 'Optim True: %s' % (time_end - time_beg) | |
def optim_false(IMAGE_PATH, IMAGE_DEST): | |
time_beg = time.time() | |
save_parameters = {} | |
image_file = '%s/%s' % (IMAGE_DEST, 'opt-false.png') | |
image_object = Image.open(IMAGE_PATH) | |
save_parameters['optimize'] = False | |
image_object.save(image_file, **save_parameters) | |
print image_object.encoderconfig | |
print image_object.encoderinfo | |
del image_object | |
time_end = time.time() | |
print 'Optim False: %s' % (time_end - time_beg) | |
def optim_normal(IMAGE_PATH, IMAGE_DEST): | |
time_beg = time.time() | |
image_file = '%s/%s' % (IMAGE_DEST, 'original-save.png') | |
image_object = Image.open(IMAGE_PATH) | |
image_object.save(image_file) | |
print image_object.encoderconfig | |
print image_object.encoderinfo | |
del image_object | |
time_end = time.time() | |
print 'Original Save: %s' % (time_end - time_beg) | |
def optim_compress(IMAGE_PATH, IMAGE_DEST, level): | |
time_beg = time.time() | |
image_file = '%s/original-compress-l%s.png' % (IMAGE_DEST, str(level)) | |
image_object = Image.open(IMAGE_PATH) | |
image_object.save(image_file, compress_level=level) | |
print image_object.encoderconfig | |
print image_object.encoderinfo | |
del image_object | |
time_end = time.time() | |
print 'Compress Level %s: %s' % (str(level), time_end - time_beg) | |
def burger_optim_true(IMAGE_PATH, IMAGE_DEST): | |
time_beg = time.time() | |
save_parameters = {} | |
image_file = '%s/%s' % (IMAGE_DEST, 'burger-true.jpg') | |
image_object = Image.open(IMAGE_PATH) | |
save_parameters['optimize'] = True | |
image_object.save(image_file, **save_parameters) | |
print image_object.encoderconfig | |
print image_object.encoderinfo | |
del image_object | |
time_end = time.time() | |
print 'Burger Optim True: %s' % (time_end - time_beg) | |
def burger_optim_normal(IMAGE_PATH, IMAGE_DEST): | |
time_beg = time.time() | |
image_file = '%s/%s' % (IMAGE_DEST, 'burger-original.jpg') | |
image_object = Image.open(IMAGE_PATH) | |
image_object.save(image_file) | |
print image_object.encoderconfig | |
print image_object.encoderinfo | |
del image_object | |
time_end = time.time() | |
print 'Original Save: %s' % (time_end - time_beg) | |
def pngjpg_optim_true(IMAGE_PATH, IMAGE_DEST): | |
time_beg = time.time() | |
save_parameters = {} | |
image_file = '%s/%s' % (IMAGE_DEST, 'pngjpg-true.jpg') | |
image_object = Image.open(IMAGE_PATH) | |
save_parameters['optimize'] = True | |
image_object.save(image_file, **save_parameters) | |
print image_object.encoderconfig | |
print image_object.encoderinfo | |
del image_object | |
time_end = time.time() | |
print 'JPGPNG Optim True: %s' % (time_end - time_beg) | |
def pngjpg_optim_normal(IMAGE_PATH, IMAGE_DEST): | |
time_beg = time.time() | |
image_file = '%s/%s' % (IMAGE_DEST, 'pngjpg-original.jpg') | |
image_object = Image.open(IMAGE_PATH) | |
image_object.save(image_file) | |
print image_object.encoderconfig | |
print image_object.encoderinfo | |
del image_object | |
time_end = time.time() | |
print 'JPGPNG Save: %s' % (time_end - time_beg) | |
if __name__ == "__main__": | |
# optim_true(IMAGE_PATH, IMAGE_DEST) | |
# optim_false(IMAGE_PATH, IMAGE_DEST) | |
# optim_normal(IMAGE_PATH, IMAGE_DEST) | |
# for level in xrange(1, 10): | |
# optim_compress(IMAGE_PATH, IMAGE_DEST, level) | |
# burger_optim_normal(JPEG_PATH, IMAGE_DEST) | |
# burger_optim_true(JPEG_PATH, IMAGE_DEST) | |
pngjpg_optim_normal(IMAGE_PATH, IMAGE_DEST) | |
pngjpg_optim_true(IMAGE_PATH, IMAGE_DEST) |
Author
karlcow
commented
May 19, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment