Python put add sentence as image in matplotlib


6 days ago


28 time


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:


1 answers


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

from PIL import Image, ImageDraw

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

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


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

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