komoot / staticmap

A small, python-based library for creating map images with lines, markers and polygons.
https://www.komoot.com
Other
290 stars 65 forks source link

fix rendering by replacing Image.ANTIALIAS with Image.LANCZOS #31

Closed SamR1 closed 1 year ago

SamR1 commented 1 year ago

Hi,

The latest release of Pillow removed some constants, including Image.ANTIALIAS, which causes this error:

    image_lines = image_lines.resize((self.width, self.height), Image.ANTIALIAS)
                                                                ^^^^^^^^^^^^^^^
AttributeError: module 'PIL.Image' has no attribute 'ANTIALIAS'

As indicated in the documentation, I have replaced it with Image.LANCZOS. The following snippet generates an image without errors:

from staticmap import Line, StaticMap

m = StaticMap(300, 400, 10)
m.headers = {"User-Agent": "Mozilla 5.0"}
line = Line([(2.3, 48.9), (4.83, 45.75)], "red", 2)
m.add_line(line)
image = m.render()
image.save("map.png")

map

christophlingg commented 1 year ago

Hi @SamR1 and thanks for your contribution. This code will work now for recent versions of PIL, will it also work for old versions? Do you know when/which version of PIL introduced Image.LANCZOS ?

SamR1 commented 1 year ago

Hi,

Thanks for your feedback.

The constant Image.ANTIALIAS was deprecated in 9.1.0 (2022-04-01): https://pillow.readthedocs.io/en/stable/releasenotes/9.1.0.html#deprecations.
The constant Image.LANCZOS was added in 2.7.0 (2015-01-01) and ANTIALIAS is left as an alias for LANCZOS : https://pillow.readthedocs.io/en/stable/releasenotes/2.7.0.html#antialias-renamed-to-lanczos.

FYI, in another branch I add a test on rendering (I didn't push the test in this pull request, since the tile server call is not mocked :/, but I can at least add GitHub Actions on existing tests, if you want). So to check rendering with different versions of Pillow, I set another branch. Rendering fails with Pillow <2.7 and python 3.8 (with a more recent version of Python, it fails with Pillow <6 due to Python version incompatibilities, see Python version matrix).

It should be OK to let Pillow version unpinned in this case since LANCZOS was added in 2.7.0 (but I don't know if there are any other incompatibilities with latest version of Pillow).

christophlingg commented 1 year ago

Thanks for checking and your detailed explanation. There is definitely no need for fixing backward compatibility with a version that is 8 years ago. I will merge now and release a new version next week when I am back from holidays and in front of my computer

christophlingg commented 1 year ago

And I’d welcome any improvement regarding ci and test coverage. This repo aged over time.

SamR1 commented 1 year ago

Thanks for your feedback! I'll open a PR with GitHub action and existing tests on supported python versions.
Have a good holiday!