From ed3ea56b5b4b5d40aa2418327a765e5f46cc3c2f Mon Sep 17 00:00:00 2001 From: Johannes Hoppe Date: Thu, 5 Jan 2017 15:38:53 +0100 Subject: [PATCH] Add default JPEG optimisation, progressive JPEGs and set quality to high_profile --- requirements-dev.txt | 1 - setup.cfg | 2 -- stdimage/models.py | 12 +++++++++++- tox.ini | 3 ++- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index 09eda4a..a69c892 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,6 +1,5 @@ -e . coverage==4.3.1 -django-coverage-plugin==1.3.1 flake8==3.2.1 isort==4.2.5 mccabe==0.5.3 diff --git a/setup.cfg b/setup.cfg index 63afc1e..8950ed9 100644 --- a/setup.cfg +++ b/setup.cfg @@ -17,8 +17,6 @@ match = (?!setup).*.py [coverage:run] source = . -plugins = - django_coverage_plugin omit = */migrations/* */tests/* diff --git a/stdimage/models.py b/stdimage/models.py index 515a8b5..d7f3345 100644 --- a/stdimage/models.py +++ b/stdimage/models.py @@ -74,6 +74,7 @@ def render_variation(cls, file_name, variation, replace=False, with storage.open(file_name) as f: with Image.open(f) as img: + save_kargs = {} file_format = img.format if cls.is_smaller(img, variation): @@ -93,6 +94,15 @@ def render_variation(cls, file_name, variation, replace=False, size = variation['width'], variation['height'] size = tuple(int(i) if i != float('inf') else i for i in size) + + if file_format == 'JPEG': + # http://stackoverflow.com/a/21669827 + img = img.convert('RGB') + save_kargs['optimize'] = True + save_kargs['quality'] = 'web_high' + if size[0] * size[1] > 10000: # roughly <10kb + save_kargs['progressive'] = True + if variation['crop']: img = ImageOps.fit( img, @@ -106,7 +116,7 @@ def render_variation(cls, file_name, variation, replace=False, ) with BytesIO() as file_buffer: - img.save(file_buffer, file_format) + img.save(file_buffer, file_format, **save_kargs) f = ContentFile(file_buffer.getvalue()) storage.save(variation_name, f) return variation_name diff --git a/tox.ini b/tox.ini index b231263..f454748 100644 --- a/tox.ini +++ b/tox.ini @@ -9,10 +9,11 @@ deps= dj110: https://github.com/django/django/archive/stable/1.10.x.tar.gz#egg=django djmaster: https://github.com/django/django/archive/master.tar.gz#egg=django commands= - py.test \ + coverage run --source=stdimage -m 'pytest' \ --basetemp={envtmpdir} \ --ignore=.tox \ {posargs} + coverage report [testenv:qa] changedir={toxinidir}