Skip to content

Commit

Permalink
Add pil version check
Browse files Browse the repository at this point in the history
Call the deprecated method `textsize` when using older pillow versions
  • Loading branch information
mironleon committed Jan 15, 2024
1 parent 33031d1 commit fac8b0e
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pygments/formatters/img.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
:copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
from packaging import version
import os
import sys

Expand All @@ -19,6 +20,7 @@
# Import this carefully
try:
from PIL import Image, ImageDraw, ImageFont
import PIL
pil_available = True
except ImportError:
pil_available = False
Expand Down Expand Up @@ -633,7 +635,11 @@ def format(self, tokensource, outfile):
fill=self.hl_color)
for pos, value, font, text_fg, text_bg in self.drawables:
if text_bg:
text_size = draw.textlength(text=value, font=font)
# see deprecations https://pillow.readthedocs.io/en/stable/releasenotes/9.2.0.html#font-size-and-offset-methods
if version.parse(PIL.__version__) < version.Version('9.2.0'):
text_size = draw.textsize(text=value, font=font)
else:
text_size = draw.textlength(text=value, font=font)
draw.rectangle([pos[0], pos[1], pos[0] + text_size[0], pos[1] + text_size[1]], fill=text_bg)
draw.text(pos, value, font=font, fill=text_fg)
im.save(outfile, self.image_format.upper())
Expand Down

0 comments on commit fac8b0e

Please sign in to comment.