-
Notifications
You must be signed in to change notification settings - Fork 20
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
Fix compatibility with Pillow 10 (Draw.textsize versus Draw.textbbox) #108
Conversation
Thanks for fixing this. We had just noticed this last week. The For the remaining failures, any idea if those are caused by the changes/differences in this textbbox usage? Or are they some other issue? |
OK I will update the patch
Actually I have always had problems with those tests in debian, but with Pillow10 they start failing also in github CI. |
Sorry, apparently some of the Draw objects around do not have the textbbox method and this sometimes results in a segfault. |
Ah I suppose this means the aggdraw version of Draw needs to have the bbox and textlength methods added to it to reflect the changes in pillow. |
Crazy idea: should we integrate aggdraw into pillow? |
We'd have to talk to the PIL maintainers. Right now there is a third-part developer who is rewriting aggdraw to be proper classes and fix various backwards incompatibilities. I think given the licensing complexities of including the C++ agg library I can't imagine pillow being open to the idea. |
Let's restart CI and see what happens. |
@avalentino if we were seg faulting before it doesn't look like that's happening, but it doesn't look like the results are equivalent either. |
Thanks @djhoese for looking into it. |
Any chance you have screenshots from before? Oh wait, you're saying they looked fine before so something else must have changed. |
No sorry I do not have screenshots or faved files. |
After breaking my compilers in my environment and trying to install aggdraw from source, I was able to reproduce the failures locally. I'm out of time for now, but I'll try to look at this tonight. |
Ok so the main issue seems to be that the text height with the new textbbox was producing a negative height. That is, in the sense that bottom was larger than top so So it says to please use |
Hm I was able to get the "germ" test to pass with the python-pillow/Pillow#4910 (comment) But I can't find a way to get that offset. |
Ok I am able to get the offset and do this calculation to reproduce the old results, but it requires using what I think is a deprecated and/or hidden API by accessing I think this is one of those cases of Pillow fixing a result that had been that way for a long time. @mraspaud @avalentino opinions on this? |
Makes replacing test images easier
I chose to replace the expected images. It seems like the best solution. |
I think there are problems with Python 3.8 pyproj on conda-forge. I'm just dropping 3.7 and 3.8 python support for pycoast and bumping the CI environments accordingly. |
@mraspaud I kind of want to also include this: Subject: [PATCH] Replace old string formatting with f-strings
---
Index: pycoast/cw_base.py
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/pycoast/cw_base.py b/pycoast/cw_base.py
--- a/pycoast/cw_base.py (revision ab950195f45375fc0f95aa879db089e81246b81a)
+++ b/pycoast/cw_base.py (date 1699042204841)
@@ -1695,19 +1695,14 @@
yield from index_arrays
def _grid_lon_label(self, lon):
- # FIXME: Use f-strings or just pass the direction
- if lon > 0.0:
- txt = "%.2dE" % (lon)
- else:
- txt = "%.2dW" % (-lon)
- return txt
+ direction = "E" if lon > 0.0 else "W"
+ lon = int(abs(round(lon)))
+ return f"{lon:02d}{direction}"
def _grid_lat_label(self, lat):
- if lat >= 0.0:
- txt = "%.2dN" % (lat)
- else:
- txt = "%.2dS" % (-lat)
- return txt
+ direction = "N" if lat >= 0.0 else "S"
+ lat = int(abs(round(lat)))
+ return f"{lat:02d}{direction}"
def _draw_grid_labels(self, draw, xys, placement_def, txt, font, **kwargs):
"""Draw text with default PIL module.""" As far as I can tell the old |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good code-wise, but the lower lon labels are kind of bothering. I think we should fix it.
pycoast/tests/grid_from_dict_pil.png
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the lower longitude labels are problematic here as part of the text disappears...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it a matter of changing the vertical aligment of the text?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah it is especially bad in this one. Good catch. I'll see if I can play with the alignment. When I tried for the original/old images it wasn't producing the same result and I didn't notice any movement in the test image labels. In this new images it looks like it is basing label location based on the center (vertical middle) or something.
@@ -317,7 +317,9 @@ def _new_test_image(mode, shape, filename, color=0): | |||
except ValueError: # the fixture wasn't used | |||
return | |||
if request.node.rep_call.failed: | |||
img.save(os.path.join(tmp_path, filename)) | |||
failed_path = tmp_path / filename | |||
print(f"Failed image saved to: {failed_path}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, very useful!
Drop 3.7 and 3.8 support
@mraspaud @pnuu and anyone else, I think I've found a bug that has existed since the lon/lat labeling was added 11 years ago. Check out: Lines 61 to 64 in 9fe2dcf
Shouldn't the So besides me wanting to fix that, I found that the above conversation with @mraspaud about the "grid_from_dict_pil.png" test result change can be resolved by multiplying the But that may only work for the particular font being used in the tests. Any other font may have other ratios/metrics. From what I can tell the main difference/change in Pillow was that text drawing is now done from the "top" anchor instead of the "ascender" anchor (I think): https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html#PIL.ImageDraw.ImageDraw.text https://pillow.readthedocs.io/en/stable/handbook/text-anchors.html#text-anchors I think text size was also changed similarly, but there may have also been an offset removed on the Y size. Also note that I'll see what I can do to produce nearly the same results regarding the bottom labels, but so far it isn't really making sense. |
Smells like a bug indeed. |
I figured it out! Even though the documentation says that textbbox and text default to an anchor of "lt" (left-top) it turns out that is only for vertical text. For "normal" horizontal text it is actually "la" (left-ascender). I was putting together an example for a pillow bug report when I noticed the difference:
So even though I was wrong about anchor not being recognized for single line text, this shows the (x1, y1, x2, y2) bounding box of the text. This is for the same position of (400, 400) but we can see that the "la" case has the bbox starting at 411 in the Y axis. So while you'd get the same height, it ignores the 11 pixels that the text is shifted due to the anchor being the "ascender". The fix is to make the txt_height be |
ab95019
to
5f401ee
Compare
@mraspaud This should be ready now. I'll re-review the test changes, but it should be good to go. This fixes both pillow 10 compatibility and the long standing (since it was added) bug for "vertically centered" lon/lat labels. They now appear on the line instead of just above it. |
There are a couple of the new test images where I don't like the new label placement, but I think these are special cases and seem to be limited to projections/areas with lon/lat lines being at extreme angles. I think this is good to go. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me! Thanks for taking the time, looks great now.
This PR fixes one of the compatibility issues with Pillow 10.0.0.
It addresses the use of the removed method
draw.textsize
Never the less, some tests still fail.
See also #107.
git diff origin/main **/*py | flake8 --diff