resize twitter and mastodon logo

This commit is contained in:
codl 2017-08-24 15:39:11 +02:00
parent effe4d1381
commit 092cc1919d
No known key found for this signature in database
GPG Key ID: 6CD7C8891ED1233A
4 changed files with 64 additions and 38 deletions

65
dodo.py
View File

@ -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(): def task_gen_logo():
"""generate versions of the logo in various sizes""" """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) widths = (200, 400, 600, 800)
formats = ('jpeg', 'webp') formats = ('jpeg', 'webp')
for width in widths: for width in widths:
for format in formats: for format in formats:
yield dict( yield dict(
name='{}.{}'.format(width, format), name='{}.{}'.format(width, format),
actions=[(resize_logo, (width, format))], actions=[(resize_image, ('logotype', width, format))],
targets=[f'static/logotype-{width}.{format}'], targets=[f'static/logotype-{width}.{format}'],
file_dep=['assets/logotype.png'], file_dep=['assets/logotype.png'],
clean=True, 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(): def task_copy_asset():
import shutil import shutil
assets = ('icon.png', 'logotype.png', 'settings.js', 'twitter.png', 'mastodon.png') assets = ('icon.png', 'logotype.png', 'settings.js')
for asset in assets: for asset in assets:
yield dict( yield dict(
name=asset, name=asset,
@ -77,10 +95,9 @@ def task_compress_static():
'static/icon.png', 'static/icon.png',
'static/logotype.png', 'static/logotype.png',
'static/settings.js', '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')))) ) + tuple((f'static/logotype-{width}.{format}' for width, format in cross((200, 400, 600, 800), ('jpeg','webp'))))
def compress_brotli(dependencies): def compress_brotli(dependencies):
for filename in dependencies: for filename in dependencies:
with open(filename, 'rb') as in_: with open(filename, 'rb') as in_:

View File

@ -1,3 +1,4 @@
{% from 'lib/picture.html' import picture %}
{% extends 'lib/layout.html' %} {% extends 'lib/layout.html' %}
{% block body %} {% block body %}
@ -30,7 +31,7 @@
<p> <p>
<a style='background-color:#1da1f2' class='btn' href="/login/twitter"> <a style='background-color:#1da1f2' class='btn' href="/login/twitter">
<span class='btn-header'> <span class='btn-header'>
<img src='{{ st('twitter.png') }}'> {{picture(st, 'twitter', (20,40,80), ('webp', 'png'))}}
</span><span class='btn-content'>Log in with Twitter</span> </span><span class='btn-content'>Log in with Twitter</span>
</a> </a>
</p> </p>
@ -42,7 +43,7 @@
<a style='background-color:#282c37' class='btn' href="{{ url_for('mastodon_login_step1', instance_url=instance.instance) }}"> <a style='background-color:#282c37' class='btn' href="{{ url_for('mastodon_login_step1', instance_url=instance.instance) }}">
{% if loop.first %} {% if loop.first %}
<span class='btn-header'> <span class='btn-header'>
<img src='{{ st('mastodon.png') }}'> {{picture(st, 'mastodon', (20,40,80), ('webp', 'png'))}}
</span><span class='btn-content'>Log in with </span><span class='btn-content'>Log in with
{% else %} {% else %}
<span class='btn-content'> <span class='btn-content'>

View File

@ -19,22 +19,12 @@
{% endif -%} {% endif -%}
{% block scripts %}{% endblock %} {% block scripts %}{% endblock %}
</head> </head>
{%- from 'lib/picture.html' import picture %}
<body> <body>
<header> <header>
<h1> <h1>
<a href="{{url_for('index')}}"> <a href="{{url_for('index')}}">
<picture> {{ picture(st, 'logotype', (200,400,600,800), ('webp','jpeg'), '200px', alt='forget') }}
<source type='image/webp' sizes='200px' srcset="
{%- for width in (200,400,600,800) -%}
{{ st('logotype-{}.webp'.format(width)) }} {{width}}w,
{%- endfor -%}
"/>
<img src="{{ st('logotype-200.jpeg') }}" alt="Forget" width='200px' height='144px' sizes='200px' srcset="
{%- for width in (200,400,600,800) -%}
{{ st('logotype-{}.jpeg'.format(width)) }} {{width}}w,
{%- endfor -%}
"/>
</picture>
</a> </a>
</h1> </h1>
</header> </header>

View File

@ -0,0 +1,18 @@
{% macro picture(st, basename, widths, formats, sizes=None, alt='') -%}
{% if not sizes %}
{% set sizes = '{}px'.format(widths[0]) %}
{% endif %}
<picture>
{%- for format in formats %}
<source type='image/{{format}}' sizes='{{sizes}}' srcset="
{%- for width in widths -%}
{{ st('{}-{}.{}'.format(basename, width, format)) }} {{width}}w,
{%- endfor -%}
"/>
{% endfor %}
<img src="{{ st('{}-{}.{}'.format(basename, widths[0], formats[-1])) }}" alt="{{alt}}" />
</picture>
{%- endmacro %}