Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Python 3.13 #650

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading