Skip to content

Instantly share code, notes, and snippets.

@karlcow
Created August 9, 2018 05:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karlcow/4d43e8f66dc67fdf919200e59f6988a9 to your computer and use it in GitHub Desktop.
Save karlcow/4d43e8f66dc67fdf919200e59f6988a9 to your computer and use it in GitHub Desktop.
from PIL import Image
import time
FILTER_LIST = [('nearest', Image.NEAREST),
('bilinear', Image.BILINEAR),
('bicubic', Image.BICUBIC),
('lanczos', Image.LANCZOS),
('antialias', Image.ANTIALIAS)]
FILE = '/Users/karl/Documents/2016/10/12/thumbnail-test/original.png'
SIZE = (500, 500)
original = Image.open(FILE)
for thumb_name, img_filter in FILTER_LIST:
# We put the cursor at the start
original2 = original.copy()
thumb_name = 'thumb_{}.jpg'.format(thumb_name)
# Creating the thumbnail
start = time.time()
original2.thumbnail(SIZE, img_filter)
middle = time.time()
original2 = original2.convert('RGB')
middle2 = time.time()
original2.save(thumb_name, 'JPEG')
end = time.time()
thumb_duration = round(middle - start, 4)
convert_duration = round(middle2 - middle, 4)
save_duration = round(end - middle2, 4)
total = round(end - start, 4)
print('''
{thumb_name}:
creating thumbnail {thumb_duration} seconds
convert to RGB {convert_duration} seconds
save thumbnail {save_duration} seconds
---------------------------------------------
total = {total} seconds
'''.format(thumb_name=thumb_name,
thumb_duration=thumb_duration,
convert_duration=convert_duration,
save_duration=save_duration,
total=total))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment