Skip to content

Commit

Permalink
Merge pull request #650 from benkonrath/python-3.13
Browse files Browse the repository at this point in the history
Add support for Python 3.13
  • Loading branch information
jrief authored Nov 9, 2024
2 parents 7020a8d + 065b74e commit 9d6fe66
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
27 changes: 24 additions & 3 deletions easy_thumbnails/optimize/post_processor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import subprocess
from imghdr import what as determinetype
from PIL import Image, UnidentifiedImageError
from django.core.files.base import ContentFile
from django.core.files.temp import NamedTemporaryFile
from easy_thumbnails.optimize.conf import settings
Expand Down Expand Up @@ -35,9 +35,30 @@ def check_output(*popenargs, **kwargs):

def optimize_thumbnail(thumbnail):
'''Optimize thumbnail images by removing unnecessary data'''
# Ignore remote storage backends.
try:
optimize_command = settings.THUMBNAIL_OPTIMIZE_COMMAND[
determinetype(thumbnail.path)]
thumbnail_path = thumbnail.path
except NotImplementedError:
return

# We can't use thumbnail.image.format directly because it's set to `None` for images
# that have been created by running a method on an existing image. i.e. It's `None`
# because of the thumnailing operations.
# https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.format
#
# Image.open() is lazy and the full file will not be read when determining the
# format.
# https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.open
try:
with Image.open(thumbnail_path) as img:
# Use the lower case version of format to match the output of previously used
# imghdr.what() (removed in Python 3.13).
format = img.format.lower()
except UnidentifiedImageError:
return

try:
optimize_command = settings.THUMBNAIL_OPTIMIZE_COMMAND[format]
if not optimize_command:
return
except (TypeError, KeyError, NotImplementedError):
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def read_files(*filenames):
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Topic :: Software Development :: Libraries :: Application Frameworks",
"Topic :: Software Development :: Libraries :: Python Modules",
],
Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ distribute = False
envlist =
py{38,39,310,311,312}-django42{-svg,}
py{310,311,312}-django50{-svg,}
py{310,311,312}-django51{-svg,}
py{310,311,312,313}-django51{-svg,}
skip_missing_interpreters = True

[gh-actions]
Expand All @@ -12,6 +12,7 @@ python =
3.10: py310
3.11: py311
3.12: py312
3.13: py313

[testenv]
setenv =
Expand Down

0 comments on commit 9d6fe66

Please sign in to comment.