Skip to content

Commit 410b472

Browse files
author
David Miguel Susano Pinto
committed
microscope.simulators: fix compatibily with PIL 10.0 (#282)
1 parent 04c15c0 commit 410b472

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

microscope/simulators/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444
_logger = logging.getLogger(__name__)
4545

4646

47+
## PIL 9.2 deprecated ImageFont.getsize and PIL 10.0 removed it in
48+
## favour of ImageFont.getbbox. When we can require PIL>=9.2, we can
49+
## remove this (issue #282)
50+
_IMAGEFONT_HAS_GETBBOX = hasattr(ImageFont.ImageFont, "getbbox")
51+
52+
4753
def _theta_generator():
4854
"""A generator that yields values between 0 and 2*pi"""
4955
TWOPI = 2 * np.pi
@@ -105,7 +111,11 @@ def get_image(self, width, height, dark=0, light=255, index=None):
105111
data = m(width, height, dark, light).astype(d)
106112
if self.numbering and index is not None:
107113
text = "%d" % index
108-
size = tuple(d + 2 for d in self._font.getsize(text))
114+
if _IMAGEFONT_HAS_GETBBOX:
115+
size = self._font.getbbox(text)[2:]
116+
else:
117+
size = self._font.getsize(text)
118+
size = tuple(d + 2 for d in size) # padding
109119
img = Image.new("L", size)
110120
ctx = ImageDraw.Draw(img)
111121
ctx.text((1, 1), text, fill=light)

0 commit comments

Comments
 (0)