compress logo as jpeg and webp

This commit is contained in:
codl 2017-08-20 20:17:38 +02:00
parent 78d8c89bd9
commit ae19b326af
No known key found for this signature in database
GPG Key ID: 6CD7C8891ED1233A
2 changed files with 41 additions and 16 deletions

40
dodo.py
View File

@ -3,22 +3,34 @@ def task_gen_logo():
from PIL import Image from PIL import Image
def resize_logo(width): def resize_logo(width, format):
with Image.open('assets/logotype.png') as im: with Image.open('assets/logotype.png') as im:
im = im.convert('L') im = im.convert('RGB')
height = im.height * width // im.width height = im.height * width // im.width
new = im.resize((width,height), resample=Image.LANCZOS) new = im.resize((width,height), resample=Image.LANCZOS)
new.save('static/logotype-{}.png'.format(width), optimize=True) 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')
for width in widths: for width in widths:
yield dict( for format in formats:
name=str(width), yield dict(
actions=[(resize_logo, (width,))], name='{}.{}'.format(width, format),
targets=[f'static/logotype-{width}.png'], actions=[(resize_logo, (width, format))],
file_dep=['assets/logotype.png'], targets=[f'static/logotype-{width}.{format}'],
clean=True, file_dep=['assets/logotype.png'],
) clean=True,
)
def task_copy_asset(): def task_copy_asset():
@ -50,6 +62,12 @@ def task_minify_css():
clean=True, clean=True,
) )
def cross(a, b):
for A in a:
for B in b:
yield (A, B)
def task_compress_static(): def task_compress_static():
import brotli import brotli
import gzip import gzip
@ -59,7 +77,7 @@ def task_compress_static():
'static/icon.png', 'static/icon.png',
'static/logotype.png', 'static/logotype.png',
'static/settings.js', 'static/settings.js',
) + tuple((f'static/logotype-{width}.png' for width in (200, 400, 600, 800))) ) + 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:

View File

@ -23,11 +23,18 @@
<header> <header>
<h1> <h1>
<a href="{{url_for('index')}}"> <a href="{{url_for('index')}}">
<img src="{{ st('logotype-200.png') }}" alt="Forget" width='200px' height='144px' sizes='200px' srcset=" <picture>
{%- for width in (200,400,600,800) -%} <source type='image/webp' sizes='200px' srcset="
{{ st('logotype-{}.png'.format(width)) }} {{width}}w, {%- for width in (200,400,600,800) -%}
{%- endfor -%} {{ 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>