Skip to content

Commit 53b5235

Browse files
committed
Fix deprecation warning about ImageFont.getsize
python-barcode/barcode/writer.py:441: DeprecationWarning: getsize is deprecated and will be removed in Pillow 10 (2023-07-01). Use getbbox or getlength instead. width, height = font.getsize(subtext) `ImageFont.getsize` is deprecated since the version 9.2 of Pillow. We use anchor instead of computing the right position to have an aligned text. It is related to the issue #163. The solution was suggested by nulano (contributor of Pillow).
1 parent 77c902f commit 53b5235

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

barcode/writer.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -438,13 +438,13 @@ def _paint_text(self, xpos, ypos):
438438
font_size = int(mm2px(pt2mm(self.font_size), self.dpi))
439439
font = ImageFont.truetype(self.font_path, font_size)
440440
for subtext in self.text.split("\n"):
441-
width, height = font.getsize(subtext)
442-
# determine the maximum width of each line
443441
pos = (
444-
mm2px(xpos, self.dpi) - width // 2,
445-
mm2px(ypos, self.dpi) - height,
442+
mm2px(xpos, self.dpi),
443+
mm2px(ypos, self.dpi),
444+
)
445+
self._draw.text(
446+
pos, subtext, font=font, fill=self.foreground, anchor="ms"
446447
)
447-
self._draw.text(pos, subtext, font=font, fill=self.foreground)
448448
ypos += pt2mm(self.font_size) / 2 + self.text_line_distance
449449

450450
def _finish(self):

0 commit comments

Comments
 (0)