Skip to content

Commit

Permalink
Merge pull request #108 from avalentino/feature/pillow-10
Browse files Browse the repository at this point in the history
Fix compatibility with Pillow 10 (Draw.textsize versus Draw.textbbox)
  • Loading branch information
mraspaud committed Nov 30, 2023
2 parents 00c32cc + d36ab30 commit 64e017e
Show file tree
Hide file tree
Showing 19 changed files with 26 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ jobs:
fail-fast: true
matrix:
os: ["windows-latest", "ubuntu-latest", "macos-latest"]
python-version: ["3.8", "3.11"]
python-version: ["3.9", "3.12"]
experimental: [false]
include:
- python-version: "3.11"
- python-version: "3.12"
os: "ubuntu-latest"
experimental: true

Expand Down
6 changes: 5 additions & 1 deletion pycoast/cw_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ def _draw_text_box(
):
"""Add a text box at position (x,y)."""
if box_outline is not None:
text_size = draw.textsize(text, font)
if hasattr(draw, "textsize"):
text_size = draw.textsize(text, font)
else:
left, top, right, bottom = draw.textbbox(text_position, text, font)
text_size = right - left, top - bottom
margin = 2
xUL = text_position[0] - margin
yUL = text_position[1]
Expand Down
13 changes: 11 additions & 2 deletions pycoast/cw_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,16 @@ def is_agg(self) -> bool:

def _draw_text(self, draw, position, txt, font, align="cc", **kwargs):
"""Draw text with agg module."""
txt_width, txt_height = draw.textsize(txt, font)
if hasattr(draw, "textsize"):
txt_width, txt_height = draw.textsize(txt, font)
else:
left, top, right, bottom = draw.textbbox(position, txt, font)
# bbox is based on "left ascender" anchor for horizontal text
# but does not include the ascender to top distance.
# In order to include that additional distance we take height from
# anchor (`position`) to the bottom of the text. See:
# https://pillow.readthedocs.io/en/stable/handbook/text-anchors.html#text-anchors
txt_width, txt_height = right - left, bottom - position[1]
x_pos, y_pos = position
ax, ay = align.lower()
if ax == "r":
Expand All @@ -170,7 +179,7 @@ def _draw_text(self, draw, position, txt, font, align="cc", **kwargs):
if ay == "b":
y_pos = y_pos - txt_height
elif ay == "c":
y_pos = y_pos - txt_width / 2
y_pos = y_pos - txt_height / 2

self._engine_text_draw(draw, x_pos, y_pos, txt, font, **kwargs)

Expand Down
Binary file modified pycoast/tests/eastern_shapes_agg.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified pycoast/tests/eastern_shapes_pil.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified pycoast/tests/grid_europe_agg_txt.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified pycoast/tests/grid_from_dict_agg.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified pycoast/tests/grid_from_dict_pil.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified pycoast/tests/grid_germ.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified pycoast/tests/grid_nh_agg.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified pycoast/tests/grid_nh_cfg_agg.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified pycoast/tests/no_h_scratch_agg.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified pycoast/tests/no_h_scratch_pil.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified pycoast/tests/no_v_scratch_agg.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified pycoast/tests/no_v_scratch_pil.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 7 additions & 15 deletions pycoast/tests/test_pycoast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
img.save(failed_path)


def images_match(ref_image, test_image):
Expand Down Expand Up @@ -1204,7 +1206,6 @@ def test_grid_agg_txt(self):
from pycoast import ContourWriterAGG

grid_img = Image.open(os.path.join(LOCAL_DIR, "grid_europe_agg_txt.png"))
grid_data = np.array(grid_img)

img = Image.new("RGB", (640, 480))
proj4_string = "+proj=stere +lon_0=8.00 +lat_0=50.00 +lat_ts=50.00 +ellps=WGS84"
Expand Down Expand Up @@ -1235,8 +1236,7 @@ def test_grid_agg_txt(self):
minor_is_tick=False,
)

res = np.array(img)
assert fft_metric(grid_data, res), "Writing of grid failed for AGG"
assert images_match(grid_img, img), "Writing of grid failed for AGG"

def test_grid_geos_agg(self):
from pycoast import ContourWriterAGG
Expand Down Expand Up @@ -1303,7 +1303,6 @@ def test_grid_nh_agg(self):
from pycoast import ContourWriterAGG

grid_img = Image.open(os.path.join(LOCAL_DIR, "grid_nh_agg.png"))
grid_data = np.array(grid_img)
img = Image.new("RGB", (425, 425))
proj4_string = "+proj=laea +lat_0=90 +lon_0=0 +a=6371228.0 +units=m"
area_extent = (-5326849.0625, -5326849.0625, 5326849.0625, 5326849.0625)
Expand All @@ -1330,12 +1329,10 @@ def test_grid_nh_agg(self):
lat_placement="",
)

res = np.array(img)

# NOTE: Experience inconsistency in ttf font writing between systems.
# Still trying to figure out why this test sometimes fails to write
# correct font markings.
assert fft_metric(grid_data, res), "Writing of nh grid failed for AGG"
assert images_match(grid_img, img), "Writing of nh grid failed for AGG"

def test_add_polygon_agg(self):
from pycoast import ContourWriterAGG
Expand Down Expand Up @@ -1497,7 +1494,6 @@ def test_config_file_coasts_and_grid(self):

overlay_config = os.path.join(LOCAL_DIR, "coasts_and_grid_agg.ini")
grid_img = Image.open(os.path.join(LOCAL_DIR, "grid_nh_cfg_agg.png"))
grid_data = np.array(grid_img)
proj_dict = {
"proj": "laea",
"lat_0": 90.0,
Expand All @@ -1513,8 +1509,7 @@ def test_config_file_coasts_and_grid(self):
img = Image.new("RGB", (850, 850), (255, 255, 255))
img.paste(overlay, mask=overlay)

res = np.array(img)
assert fft_metric(grid_data, res), "Writing of nh grid failed"
assert images_match(grid_img, img), "Writing of nh cfg grid failed"

@pytest.mark.usefixtures("cd_test_dir")
def test_config_file_points_and_borders_agg(self):
Expand Down Expand Up @@ -1770,8 +1765,6 @@ def test_add_grid_from_dict_agg(self):

grid_img = Image.open(os.path.join(LOCAL_DIR, "grid_from_dict_agg.png"))

grid_data = np.array(grid_img)

img = Image.new("RGB", (800, 800))
proj4_string = "+proj=stere +ellps=WGS84 +lon_0=-4.532 +lat_0=54.228"
area_extent = (-600000.0, -600000.0, 600000.0, 600000.0)
Expand Down Expand Up @@ -1809,8 +1802,7 @@ def test_add_grid_from_dict_agg(self):

img = cw.add_overlay_from_dict(overlays, area_def, background=img)

res = np.array(img)
assert fft_metric(grid_data, res), "Writing grid from dict agg failed"
assert images_match(grid_img, img), "Writing grid from dict agg failed"

def test_lonlat_pm_change(self):
"""Test that a longlat projection with a non-0 prime meridian is handled correctly."""
Expand Down
Binary file modified pycoast/tests/western_shapes_agg.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified pycoast/tests/western_shapes_pil.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
include_package_data=True,
install_requires=requires,
extras_require=extras_require,
python_requires=">3.7",
python_requires=">3.9",
zip_safe=False,
classifiers=[
"Development Status :: 5 - Production/Stable",
Expand Down

0 comments on commit 64e017e

Please sign in to comment.