Repository: scrapy/scrapy

Issue:
DeprecationWarning: ANTIALIAS is deprecated
> scrapy/pipelines/images.py:163: DeprecationWarning: ANTIALIAS is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.LANCZOS instead.
>    image.thumbnail(size, self._Image.ANTIALIAS)

This is deprecated since Pillow 9.1.0, released in April. We should check if `Resampling.LANCZOS` is already available in the earliest version we support (7.1.0 as far as I can see), and use it if it's available there. If it was added later, I think we need to decide how to proceed.


Discussion:
@wRAR  I just checked `Resampling.LANCZOS` is not available in the Pillow 7.1.0.

How should we proceed?
@iamkaushal I see.
Probably use `Resampling.LANCZOS` if it's available, otherwise use whatever is available instead (assuming `Image.LANCZOS` is available on all suppported versions, I'd use it instead of `Image.ANTIALIAS` to make it clearer that the code does the same on all versions, as AFAICS `Image.LANCZOS` is the same as `Image.ANTIALIAS`).
`Image.LANCZOS` is not available since version 9.1.0 @wRAR  
It's available, of course. But even if it wasn't, I don't see how it would prevent implementing what I wrote earlier.
Yes sorry I meant it is not available without warning. If replacing with `Image.LANCZOS` is fine for now, I would like to work on this issue. 
I think we can just switch to the new name, [it seems it has been available since Pillow 2.7](https://pillow.readthedocs.io/en/stable/releasenotes/2.7.0.html#antialias-renamed-to-lanczos).
> Yes sorry I meant it is not available without warning.

But I said "use Resampling.LANCZOS if it's available".

> I think we can just switch to the new name, it seems it has been available since Pillow 2.7.

It's only about Image.LANCZOS which is deprecated in favor of Image.Resampling.LANCZOS as well.
@iamkaushal If you are up for it, the strategy here would be to do something like:

```
try:
    LANCZOS = Image.Resampling.LANCZOS
except AttributeError:
    LANCZOS = Image.LANCZOS
```

And then use `LANCZOS` in Scrapy code.

I am not `AttributeError` is the actual exception raised, mind you.
@Gallaecio  Thank you. Yes I would like to take this up. 

 
> But I said "use Resampling.LANCZOS if it's available".

@wRAR  My bad, I got confused because `Resampling.LANCZOS` is not available in 7.1.0

I want to work on this based on the idea proposed by @Gallaecio 

@Gallaecio  @wRAR I am new to open source. Really appreciate your support. Thank you. 

Please also add a comment which tells in which version was `Image.Resampling.LANCZOS` added, so that we can see if we can delete the try-except block when updating the minimum reqs for Pillow.
Sure, will do it. 

Resolving patch:
diff --git a/scrapy/pipelines/images.py b/scrapy/pipelines/images.py
--- a/scrapy/pipelines/images.py
+++ b/scrapy/pipelines/images.py
@@ -160,7 +160,14 @@ def convert_image(self, image, size=None):
 
         if size:
             image = image.copy()
-            image.thumbnail(size, self._Image.ANTIALIAS)
+            try:
+                # Image.Resampling.LANCZOS was added in Pillow 9.1.0
+                # remove this try except block,
+                # when updating the minimum requirements for Pillow.
+                resampling_filter = self._Image.Resampling.LANCZOS
+            except AttributeError:
+                resampling_filter = self._Image.ANTIALIAS
+            image.thumbnail(size, resampling_filter)
 
         buf = BytesIO()
         image.save(buf, 'JPEG')


Test patch: