From 092cc1919d900b950f6e3d20ace75ec3dc0b6ebe Mon Sep 17 00:00:00 2001 From: codl Date: Thu, 24 Aug 2017 15:39:11 +0200 Subject: [PATCH] resize twitter and mastodon logo --- dodo.py | 65 ++++++++++++++++++++++++-------------- templates/index.html | 5 +-- templates/lib/layout.html | 14 ++------ templates/lib/picture.html | 18 +++++++++++ 4 files changed, 64 insertions(+), 38 deletions(-) create mode 100644 templates/lib/picture.html diff --git a/dodo.py b/dodo.py index ee7bd0e..7852ba1 100644 --- a/dodo.py +++ b/dodo.py @@ -1,41 +1,59 @@ +def resize_image(basename, width, format): + from PIL import Image + with Image.open('assets/{}.png'.format(basename)) as im: + if 'A' in im.getbands() and format != 'jpeg': + im = im.convert('RGBA') + else: + im = im.convert('RGB') + height = im.height * width // im.width + new = im.resize((width,height), resample=Image.LANCZOS) + if format == 'jpeg': + kwargs = dict( + optimize = True, + progressive = True, + quality = 80, + ) + elif format == 'webp': + kwargs = dict( + quality = 79, + ) + elif format == 'png': + kwargs = dict( + optimize = True, + ) + new.save('static/{}-{}.{}'.format(basename, width, format), **kwargs) + def task_gen_logo(): """generate versions of the logo in various sizes""" - - from PIL import Image - - def resize_logo(width, format): - with Image.open('assets/logotype.png') as im: - im = im.convert('RGB') - height = im.height * width // im.width - new = im.resize((width,height), resample=Image.LANCZOS) - if format == 'jpeg': - kwargs = dict( - optimize = True, - progressive = True, - quality = 80, - ) - elif format == 'webp': - kwargs = dict( - quality = 79, - ) - new.save('static/logotype-{}.{}'.format(width, format), **kwargs) - widths = (200, 400, 600, 800) formats = ('jpeg', 'webp') for width in widths: for format in formats: yield dict( name='{}.{}'.format(width, format), - actions=[(resize_logo, (width, format))], + actions=[(resize_image, ('logotype', width, format))], targets=[f'static/logotype-{width}.{format}'], file_dep=['assets/logotype.png'], clean=True, ) +def task_button_icons(): + widths = (20,40,80) + formats = ('webp', 'png') + for width in widths: + for format in formats: + for basename in ('twitter', 'mastodon'): + yield dict( + name='{}-{}.{}'.format(basename, width, format), + actions=[(resize_image, (basename, width, format))], + targets=['static/{}-{}.{}'.format(basename,width,format)], + file_dep=['assets/{}.png'.format(basename)], + clean=True, + ) def task_copy_asset(): import shutil - assets = ('icon.png', 'logotype.png', 'settings.js', 'twitter.png', 'mastodon.png') + assets = ('icon.png', 'logotype.png', 'settings.js') for asset in assets: yield dict( name=asset, @@ -77,10 +95,9 @@ def task_compress_static(): 'static/icon.png', 'static/logotype.png', 'static/settings.js', - 'static/twitter.png', - 'static/mastodon.png', ) + tuple((f'static/logotype-{width}.{format}' for width, format in cross((200, 400, 600, 800), ('jpeg','webp')))) + def compress_brotli(dependencies): for filename in dependencies: with open(filename, 'rb') as in_: diff --git a/templates/index.html b/templates/index.html index 76f3cfb..7a14611 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,3 +1,4 @@ +{% from 'lib/picture.html' import picture %} {% extends 'lib/layout.html' %} {% block body %} @@ -30,7 +31,7 @@

- + {{picture(st, 'twitter', (20,40,80), ('webp', 'png'))}} Log in with Twitter

@@ -42,7 +43,7 @@ {% if loop.first %} - + {{picture(st, 'mastodon', (20,40,80), ('webp', 'png'))}} Log in with {% else %} diff --git a/templates/lib/layout.html b/templates/lib/layout.html index 7b1a175..5fd3a7a 100644 --- a/templates/lib/layout.html +++ b/templates/lib/layout.html @@ -19,22 +19,12 @@ {% endif -%} {% block scripts %}{% endblock %} +{%- from 'lib/picture.html' import picture %}

- - - Forget - + {{ picture(st, 'logotype', (200,400,600,800), ('webp','jpeg'), '200px', alt='forget') }}

diff --git a/templates/lib/picture.html b/templates/lib/picture.html new file mode 100644 index 0000000..600c8c0 --- /dev/null +++ b/templates/lib/picture.html @@ -0,0 +1,18 @@ +{% macro picture(st, basename, widths, formats, sizes=None, alt='') -%} +{% if not sizes %} +{% set sizes = '{}px'.format(widths[0]) %} +{% endif %} + + + + {%- for format in formats %} + + {% endfor %} + {{alt}} + + +{%- endmacro %}