Skip to content

Fix Pillow 10.x compatibility with targeted textsize patch instead of global monkey patching #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions function/fastapi_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,45 @@
import fastapi
import fastapi.responses
import staticmaps
from PIL import ImageDraw


def _pillow_textsize_compat(self, text, font=None, *args, **kwargs):
"""
Compatibility function for Pillow 10.x that provides the old textsize() behavior.

This function uses the new textbbox() method and converts the result to the
(width, height) tuple that textsize() used to return.

The textsize() method was removed in Pillow 10.0.0 and replaced with textbbox().
See: https://github.com/python-pillow/Pillow/pull/6474
Migration guide: https://pillow.readthedocs.io/en/stable/releasenotes/10.0.0.html#font-size-and-offset-methods

Args:
text: The text to measure
font: The font to use (optional)
*args, **kwargs: Additional arguments passed to textbbox

Returns:
tuple: (width, height) of the text
"""
# Use textbbox with anchor point (0, 0) and convert to size
bbox = self.textbbox((0, 0), text, font=font, *args, **kwargs)
# bbox is (left, top, right, bottom), so width = right - left, height = bottom - top
return (bbox[2] - bbox[0], bbox[3] - bbox[1])


def _ensure_pillow_textsize_compatibility():
"""
Ensure textsize() method is available for py-staticmaps compatibility.

This function patches ImageDraw.ImageDraw to add the textsize() method
if it doesn't exist (i.e., in Pillow 10.x). This is needed because
py-staticmaps 0.4.0 uses the deprecated textsize() method.
"""
if not hasattr(ImageDraw.ImageDraw, "textsize"):
ImageDraw.ImageDraw.textsize = _pillow_textsize_compat


router = fastapi.APIRouter()

Expand All @@ -28,6 +67,9 @@ def generate_map(
height: int = 400,
tile_provider: TileProvider = TileProvider.osm,
) -> ImageResponse:
# Ensure Pillow 10.x compatibility for py-staticmaps
_ensure_pillow_textsize_compatibility()

# Create the static map context
context = staticmaps.Context()
context.set_tile_provider(staticmaps.default_tile_providers[tile_provider.value])
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
azure-functions>=1.12.0
py-staticmaps==0.4.0
fastapi==0.111.0
Pillow==9.5.0
Pillow==10.3.0
nest-asyncio==1.6.0
opentelemetry-instrumentation-fastapi==0.48b0
azure-monitor-opentelemetry-exporter==1.0.0b29