-
Notifications
You must be signed in to change notification settings - Fork 8
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
a2f4090
8aa750f
0d6d4fd
87c37c2
51a8e84
bad426c
1695cb4
47cbcb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -133,3 +133,6 @@ __queuestorage__ | |
| __azurite_db*__.json | ||
| .python_packages | ||
| .azure | ||
|
|
||
| # Test scripts | ||
pamelafox marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| test_pillow_compatibility.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"): | ||
|
||
| ImageDraw.ImageDraw.textsize = textsize | ||
Uh oh!
There was an error while loading. Please reload this page.