Reputation: 1165
How to set the same font like the LaTeX uses? I tried the following code. I used a similar way I use for matplotlib.
from PIL import Image, ImageFilter
from PIL import Image, ImageDraw, ImageFont
import matplotlib.pyplot as plt
plt.rcParams['text.latex.preamble'] = [r"\usepackage{bm}", r"\usepackage{amsmath}"]
params = {'text.usetex' : True,
'font.size' : 25,
'font.family' : 'lmodern',
'text.latex.unicode': True,
}
plt.rcParams.update(params)
im = Image.new("RGB", (512, 512), (128, 128, 128))
draw = ImageDraw.Draw(im)
draw.multiline_text((100, 1000), 'Pillow sample', fill=(0, 0, 0), font=params)
im.show()
I got the following error:
draw.multiline_text((100, 1000), 'Pillow sample', fill=(0, 0, 0), font=params)
File "/usr/lib/python3/dist-packages/PIL/ImageDraw.py", line 234, in multiline_text
line_spacing = self.textsize('A', font=font)[1] + spacing
File "/usr/lib/python3/dist-packages/PIL/ImageDraw.py", line 263, in textsize
return font.getsize(text, direction, features)
AttributeError: 'dict' object has no attribute 'getsize'
Upvotes: 1
Views: 1375
Reputation: 6858
The variable params
you use as argument for argument font
in multiline_text
is a dictionary, while that function expects a font object. So you first need to create a font object from those parameters. See docs.
fnt = ImageFont.truetype("Pillow/Tests/fonts/FreeMono.ttf", 40)
Upvotes: 1