python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
12.24k stars 2.23k forks source link

Where is source code for LANCZOS etc located? #6257

Closed klappec-bsci closed 2 years ago

klappec-bsci commented 2 years ago

I found Image.resize() starting on line 1922 of https://github.com/python-pillow/Pillow/blob/bcded33ceead2ae69dad602ce95d8f892ffb46df/src/PIL/Image.py

but .resize() doesn't contain the 'logic' that actually does the calculations for each pixel. I can't find anything resize() calls that actually does contain that logic for executing the math behind the resizing. Is it in some file I missed?

I need to reimplement PIL's exact lanczos algorithm in tensorflow for reasons. (The opencv2 and tf native lanczos algorithms are slightly different)

Any insight?

nulano commented 2 years ago

The internal C function for resize https://github.com/python-pillow/Pillow/blob/44bb3e62df5ea5dc7a401018ec852e7253879ccd/src/_imaging.c#L1786 calls ImagingResample in Resample.c which resizes the image using a Lanczos kernel computed in https://github.com/python-pillow/Pillow/blob/44bb3e62df5ea5dc7a401018ec852e7253879ccd/src/libImaging/Resample.c#L73-L80

radarhere commented 2 years ago

You might be interested in looking at #6200

radarhere commented 2 years ago

@klappec-bsci did that help?

klappec-bsci commented 2 years ago

@radarhere yes! kind of. so thank you.I did find the source code thanks to you. My goal was to re-implement PIL's exact specific LANCZOS algorithm in Tensorflow. but I think this might be beyond my current skills (:

radarhere commented 2 years ago

I can appreciate that you might have expected to find Python code and instead found C code.

An alternate solution to your situation might be to convert images between Pillow and Tensorflow, but perhaps you've already considered that or it's not applicable.

klappec-bsci commented 2 years ago

we have a neural network trained using PIL resize, but resizing on the gpu would be faster. PIL resize is too slow for our needs. deploying the AI model, we need the same resize. tf.image.resize lanczos3 is close but not quite the same.

radarhere commented 2 years ago

Ok. Let us know if there is anything else we can do.

klappec-bsci commented 2 years ago

Thanks so much you guys are amazing. is there a textbook or a reference you used when implementing the lanczos algorithm? something you followed as a reference?

radarhere commented 2 years ago

It has been present since before Pillow was forked from PIL (just called ANTIALIAS then), so I can't answer that unfortunately.

There have been naturally been changes to the code since, like #1881 improving speed.

I think https://uploadcare.com/blog/the-fastest-image-resize/#convolution-based-resampling is a good long form explanation of what is happening.

klappec-bsci commented 2 years ago

Thank you SO much. This helped a lot, I never expected this level of support. Closed.