JohannesBuchner / imagehash

A Python Perceptual Image Hashing Module
BSD 2-Clause "Simplified" License
3.14k stars 328 forks source link

#164: replaced deprecated `Image.ANTIALIAS` with `Image.Resampling.LANCZOS` #169

Closed lene closed 2 years ago

lene commented 2 years ago

Closes #164

JohannesBuchner commented 2 years ago

Does this patch lead to compatible behaviour over different versions of Pillow?

coveralls commented 2 years ago

Coverage Status

Coverage decreased (-1.8%) to 87.097% when pulling e8dc2bafe9e7cc69fc2d6fc54dfb0314d95652b0 on lene:164-update-deprecated-image-antialias into 9b7bcf78cfcaf6f04ff8390bb81a2f94abb4df7c on JohannesBuchner:master.

Avasam commented 2 years ago

Does this patch lead to compatible behaviour over different versions of Pillow?

No because of the Pillow version specifier that was added. https://github.com/python-pillow/Pillow/releases/tag/9.0.0 dropped support for Python 3.6 and below This should probably instead dynamically check for the installed pillow version in code then use the appropriate image resampling constant. As simple as:

from PIL import Image, __version__
__PIL_VERSION = tuple(map(int, __version__.split('.')))
if __PIL_VERSION >= (9, 1):
  _RESAMPLING = Image.Resampling.LANCZOS
else:
  _RESAMPLING = Image.ANTIALIAS
JohannesBuchner commented 2 years ago

I am worried that if someone uses imagehash with some version of pillow, and now reinstalls imagehash with this PR applied, that the previously computed hashes do not match the same images any more, if the aliasing behaviour is different.

That's why I am wondering: is there is a pillow flag which brings back the exact aliasing behaviour of Image.ANTIALIAS?

JohannesBuchner commented 2 years ago

https://pillow.readthedocs.io/en/stable/deprecations.html suggests that it is a rename

two users provided anecdotes that the behaviour is the same: https://www.reddit.com/r/learnpython/comments/uxb8b4/help_pillow_deprecation_warning/

JohannesBuchner commented 2 years ago

I don't like the version checks, we should minimize this as much as possible, in favor of behaviour checks.

Maybe something like:

try:
    ANTIALIAS = Image.Resampling.LANCZOS
except AttributeError:
    ANTIALIAS = Image.ANTIALIAS
Avasam commented 2 years ago

I don't like the version checks, we should minimize this as much as possible, in favor of behaviour checks.

Maybe something like:

try:
    ANTIALIAS = Image.Resampling.LANCZOS
except AttributeError:
    ANTIALIAS = Image.ANTIALIAS

Oh yeah that's much better. Sometimes I forget that exceptions are totally valid for flow control in Python.

JohannesBuchner commented 2 years ago

This is fixed in master.