Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,6 @@ __queuestorage__
__azurite_db*__.json
.python_packages
.azure

# Test scripts
test_pillow_compatibility.py
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove from .gitignore

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Remove that line from .gitignore

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the test_pillow_compatibility.py line from .gitignore. Commit e8b6c9a.

3 changes: 3 additions & 0 deletions function/fastapi_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor

# Import Pillow 10.x compatibility patch for py-staticmaps
import pillow_textsize_compat # noqa: F401

from . import fastapi_routes


Expand Down
37 changes: 37 additions & 0 deletions pillow_textsize_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
Pillow 10.x compatibility patch for py-staticmaps.
This module provides a monkey patch to restore the textsize() method
that was removed in Pillow 10.0.0 and replaced with textbbox().
This ensures compatibility with py-staticmaps 0.4.0 while maintaining
the security benefits of Pillow 10.3.0.
"""

from PIL import ImageDraw


def textsize(self, text, font=None, *args, **kwargs):
"""
Compatibility method for Pillow 10.x that provides the old textsize() behavior.
This method uses the new textbbox() method and converts the result to the
(width, height) tuple that textsize() used to return.
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])


# Only patch if textsize doesn't exist (i.e., Pillow 10.x)
if not hasattr(ImageDraw.ImageDraw, "textsize"):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Instead of monkey patching, which can be brittle, move this function into the file that needs it, and just directly call this function.
Add a link to any Pillow issues on GitHub that discuss this as the best way to upgrade past uses of textsize.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced the global monkey patch with a targeted approach. The textsize compatibility function is now in fastapi_routes.py and only patches ImageDraw.ImageDraw locally when needed, right before using py-staticmaps. Also added links to the relevant Pillow GitHub PR (#6474) and migration guide. Commit 51a8e84.

ImageDraw.ImageDraw.textsize = textsize
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
Loading