Python put add sentence as image in matplotlib

Refresh

6 days ago

Views

28 time

-3

I have a text:

"Hello world"

and a heat map as a 2d-array.

I want to draw the text as a graph in order to put the heatmap on top of it.

So, my question is how to convert the string "Hello world" to an image in order to plot it?

For example, I want to create this .png:

out

1 answers

0

Сохранение изображения с текстом:

from PIL import Image, ImageDraw

img = Image.new('RGB', (200, 100))
d = ImageDraw.Draw(img)
d.text((20, 20), 'Hello World', fill=(255, 0, 0))
img.save("image.png")

а затем вы можете построить против него.

РЕДАКТИРОВАТЬ:

добавить шрифт типа:

d.text((20, 20), 'Hello World', fill=(255, 0, 0), font=ImageFont.truetype("arial"))