From 804e280ab3ab1ffe1785c0e8b0b209080835600c Mon Sep 17 00:00:00 2001 From: amirreza Date: Mon, 14 Oct 2024 19:53:44 +0330 Subject: [PATCH 1/6] added avif dependency --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 4133e02d2..200428d72 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -44,6 +44,7 @@ Repository = "https://github.com/jazzband/sorl-thumbnail" pgmagick = ["pgmagick"] pil = ["pillow"] wand = ["wand"] +avif = ["pillow-avif-plugin"] [tool.coverage.run] source = ["sorl"] From 328a240bdb42ce510e8998f4501f3e5d48573a65 Mon Sep 17 00:00:00 2001 From: amirreza Date: Mon, 14 Oct 2024 19:54:15 +0330 Subject: [PATCH 2/6] added avif support --- sorl/thumbnail/base.py | 3 +++ sorl/thumbnail/engines/pil_engine.py | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/sorl/thumbnail/base.py b/sorl/thumbnail/base.py index 2589db54e..d9c5e0aea 100644 --- a/sorl/thumbnail/base.py +++ b/sorl/thumbnail/base.py @@ -16,6 +16,7 @@ 'PNG': 'png', 'GIF': 'gif', 'WEBP': 'webp', + 'AVIF': 'avif', } @@ -57,6 +58,8 @@ def _get_format(self, source): return 'GIF' elif file_extension == '.webp': return 'WEBP' + elif file_extension == '.avif': + return 'AVIF' else: from django.conf import settings diff --git a/sorl/thumbnail/engines/pil_engine.py b/sorl/thumbnail/engines/pil_engine.py index ea754fd77..c9e927e46 100644 --- a/sorl/thumbnail/engines/pil_engine.py +++ b/sorl/thumbnail/engines/pil_engine.py @@ -9,6 +9,10 @@ import ImageDraw import ImageFile import ImageMode +try: + import pillow_avif +except ImportError: + pass if hasattr(Image, 'Resampling'): ANTIALIAS = Image.Resampling.LANCZOS From 5c22c605869a1c85036852f6ca2a5fade9bcf30a Mon Sep 17 00:00:00 2001 From: amirreza Date: Mon, 14 Oct 2024 19:54:31 +0330 Subject: [PATCH 3/6] added a test --- tests/thumbnail_tests/test_backends.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/thumbnail_tests/test_backends.py b/tests/thumbnail_tests/test_backends.py index b4116c1ff..864ecaff2 100644 --- a/tests/thumbnail_tests/test_backends.py +++ b/tests/thumbnail_tests/test_backends.py @@ -56,6 +56,7 @@ def test_with_various_formats(self): self.assertEqual(self.backend._get_format(FakeFile('foo.jpeg')), 'JPEG') self.assertEqual(self.backend._get_format(FakeFile('foo.png')), 'PNG') self.assertEqual(self.backend._get_format(FakeFile('foo.gif')), 'GIF') + self.assertEqual(self.backend._get_format(FakeFile('foo.avif')), 'AVIF') def test_double_extension(self): self.assertEqual(self.backend._get_format(FakeFile('foo.ext.jpg')), 'JPEG') From 0498bc3c6f5ce0baa35bc6e28f28232d36f827a5 Mon Sep 17 00:00:00 2001 From: amirreza Date: Mon, 14 Oct 2024 20:19:22 +0330 Subject: [PATCH 4/6] added avif to docs --- docs/installation.rst | 1 + docs/reference/settings.rst | 4 ++-- docs/requirements.rst | 4 ++++ docs/template.rst | 17 +++++++++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/docs/installation.rst b/docs/installation.rst index 4f875384b..b1d737691 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -12,6 +12,7 @@ sorl-thumbnail:: Depending of the chosen image backend, you may also use one of:: pip install sorl-thumbnail[pil] + pip install sorl-thumbnail[pil, avif] pip install sorl-thumbnail[wand] pip install sorl-thumbnail[pgmagick] diff --git a/docs/reference/settings.rst b/docs/reference/settings.rst index 3988f6ad2..8aa026d35 100644 --- a/docs/reference/settings.rst +++ b/docs/reference/settings.rst @@ -281,7 +281,7 @@ The generated thumbnails filename prefix. - Default: ``'JPEG'`` -Default image format, supported formats are: ``'JPEG'``, ``'PNG'``. This also implicitly +Default image format, supported formats are: ``'JPEG'``, ``'PNG'``, ``'GIF'``, ``'WEBP'``, ``'AVIF'``. This also implicitly sets the filename extension. This can be overridden by individual options. ``THUMBNAIL_PRESERVE_FORMAT`` @@ -358,7 +358,7 @@ missing input source. This is the generated thumbnail whensource of the presented thumbnail. Width and Height is passed to the string for formatting. Other options are for example: -- ``https://via.placeholder.com/%(width)sx%(height)s +- ``https://via.placeholder.com/%(width)sx%(height)s`` - ``http://placekitten.com/%(width)s/%(height)s`` diff --git a/docs/requirements.rst b/docs/requirements.rst index c39851f69..d536d264d 100644 --- a/docs/requirements.rst +++ b/docs/requirements.rst @@ -73,6 +73,10 @@ want to see the following:: --- JPEG support available --- ZLIB (PNG/ZIP) support available + +to activate avif support install the plugin:: + + pip install sorl-thumbnail[avif, pil] pgmagick installation --------------------- diff --git a/docs/template.rst b/docs/template.rst index 8e3dc4f36..df77cd7ad 100644 --- a/docs/template.rst +++ b/docs/template.rst @@ -65,6 +65,23 @@ these things out:: else: self.storage = default_storage +Format +------ + +sorl-thumbnail supports these formats: + +#. jpg, jpeg +#. png +#. gif +#. webp +#. avif (requires pillow and pillow-avif-plugin) + +to use different format for your images set this variable in your settings. + +.. code-block:: python + + THUMBNAIL_FORMAT = 'JPEG' + Geometry -------- From 208af1262a875baa510b2b623e365f46dfdc358e Mon Sep 17 00:00:00 2001 From: amirreza Date: Mon, 14 Oct 2024 20:28:29 +0330 Subject: [PATCH 5/6] tox deps --- tox.ini | 1 + 1 file changed, 1 insertion(+) diff --git a/tox.ini b/tox.ini index af890bde0..b34ba6648 100644 --- a/tox.ini +++ b/tox.ini @@ -31,6 +31,7 @@ envlist = deps = coverage[toml] pillow + pillow-avif-plugin redis: redis dynamodb: boto pgmagick: pgmagick From 9981cba3a6165c8c228e0b877d2b19567b772d0d Mon Sep 17 00:00:00 2001 From: amirreza Date: Mon, 14 Oct 2024 20:35:14 +0330 Subject: [PATCH 6/6] ignore unused import warning pillow_avif is a plugin, there is nothing to be used --- sorl/thumbnail/engines/pil_engine.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorl/thumbnail/engines/pil_engine.py b/sorl/thumbnail/engines/pil_engine.py index c9e927e46..0eb4e4b60 100644 --- a/sorl/thumbnail/engines/pil_engine.py +++ b/sorl/thumbnail/engines/pil_engine.py @@ -10,7 +10,7 @@ import ImageFile import ImageMode try: - import pillow_avif + import pillow_avif # noqa: F401 except ImportError: pass