Fix XYZ grid with Pillow >=10.0.0
This commit is contained in:
+14
-3
@@ -13,6 +13,8 @@ import numpy as np
|
|||||||
import piexif
|
import piexif
|
||||||
import piexif.helper
|
import piexif.helper
|
||||||
from PIL import Image, ImageFont, ImageDraw, ImageColor, PngImagePlugin, ImageOps
|
from PIL import Image, ImageFont, ImageDraw, ImageColor, PngImagePlugin, ImageOps
|
||||||
|
from PIL import __version__ as pillow_version
|
||||||
|
from pkg_resources import parse_version
|
||||||
# pillow_avif needs to be imported somewhere in code for it to work
|
# pillow_avif needs to be imported somewhere in code for it to work
|
||||||
import pillow_avif # noqa: F401
|
import pillow_avif # noqa: F401
|
||||||
import string
|
import string
|
||||||
@@ -168,9 +170,18 @@ def draw_grid_annotations(im, width, height, hor_texts, ver_texts, margin=0):
|
|||||||
for line in lines:
|
for line in lines:
|
||||||
fnt = initial_fnt
|
fnt = initial_fnt
|
||||||
fontsize = initial_fontsize
|
fontsize = initial_fontsize
|
||||||
while drawing.multiline_textsize(line.text, font=fnt)[0] > line.allowed_width and fontsize > 0:
|
if parse_version(pillow_version) >= parse_version('10.0.0'):
|
||||||
fontsize -= 1
|
# New code for Pillow 10.0.0+
|
||||||
fnt = get_font(fontsize)
|
text_width, text_height = drawing.multiline_textbbox((0, 0), line.text, font=fnt)[2:]
|
||||||
|
while text_width > line.allowed_width and fontsize > 0:
|
||||||
|
fontsize -= 1
|
||||||
|
fnt = get_font(fontsize)
|
||||||
|
text_width, text_height = drawing.multiline_textbbox((0, 0), line.text, font=fnt)[2:]
|
||||||
|
else:
|
||||||
|
# Old code for Pillow versions below 10.0.0
|
||||||
|
while drawing.multiline_textsize(line.text, font=fnt)[0] > line.allowed_width and fontsize > 0:
|
||||||
|
fontsize -= 1
|
||||||
|
fnt = get_font(fontsize)
|
||||||
drawing.multiline_text((draw_x, draw_y + line.size[1] / 2), line.text, font=fnt, fill=color_active if line.is_active else color_inactive, anchor="mm", align="center")
|
drawing.multiline_text((draw_x, draw_y + line.size[1] / 2), line.text, font=fnt, fill=color_active if line.is_active else color_inactive, anchor="mm", align="center")
|
||||||
|
|
||||||
if not line.is_active:
|
if not line.is_active:
|
||||||
|
|||||||
Reference in New Issue
Block a user