Update the code to not use deprecated constants on newer Pillow versions

This commit is contained in:
Fabien LOISON 2022-10-12 14:33:20 +02:00
parent 5818d7a984
commit 26635f0bd3
No known key found for this signature in database
GPG Key ID: FF90CA148348048E
3 changed files with 26 additions and 9 deletions

View File

@ -56,7 +56,7 @@ Changelog
* **[NEXT]** (changes on ``master`` that have not been released yet):
* Nothing yet
* Update the code to not use deprecated constants on newer Pillow versions
* **1.2.1:**

View File

@ -236,6 +236,15 @@ from .options import normalize_options
from . import helpers
# Since Pillow v9.1.0, constants on the Image object are deprecated and will be
# removed in Pillow v10.0.0. This code ansure the compatibility with all
# versions.
# See: https://pillow.readthedocs.io/en/stable/deprecations.html#constants
Resampling = Image
if hasattr(Image, "Resampling"):
Resampling = Image.Resampling
def optimize(input_file, output_file, options={}, verbose=False, quiet=False):
"""Optimize given image.
@ -273,7 +282,7 @@ def optimize(input_file, output_file, options={}, verbose=False, quiet=False):
# Resize image if requested
if options["resize"] != "orig":
image.thumbnail(options["resize"], Image.LANCZOS)
image.thumbnail(options["resize"], Resampling.LANCZOS)
# Quantize if requested
if options["enable_quantization"]:

View File

@ -3,16 +3,24 @@ import mozjpeg_lossless_optimization
from PIL import Image
# Since Pillow v9.1.0, constants on the Image object are deprecated and will be
# removed in Pillow v10.0.0. This code ansure the compatibility with all
# versions.
# See: https://pillow.readthedocs.io/en/stable/deprecations.html#constants
Transpose = Image
if hasattr(Image, "Transpose"):
Transpose = Image.Transpose
EXIF_TAG_ORIENTATION = 0x0112
ORIENTATION_OPERATIONS = {
1: [],
2: [Image.FLIP_LEFT_RIGHT],
3: [Image.ROTATE_180],
4: [Image.FLIP_TOP_BOTTOM],
5: [Image.FLIP_LEFT_RIGHT, Image.ROTATE_90],
6: [Image.ROTATE_270],
7: [Image.FLIP_LEFT_RIGHT, Image.ROTATE_270],
8: [Image.ROTATE_90],
2: [Transpose.FLIP_LEFT_RIGHT],
3: [Transpose.ROTATE_180],
4: [Transpose.FLIP_TOP_BOTTOM],
5: [Transpose.FLIP_LEFT_RIGHT, Transpose.ROTATE_90],
6: [Transpose.ROTATE_270],
7: [Transpose.FLIP_LEFT_RIGHT, Transpose.ROTATE_270],
8: [Transpose.ROTATE_90],
}