0

I want to create an image of individual letters using python/numpy.

Currently I've got the following which works but is probably very inefficient as it needs to save an image everytime.

plt.imshow(np.ones([2, 2, 3]))
plt.gca().axis('off')
plt.text(0, 0, 'hello', color='k', fontsize='18')
plt.gcf().savefig('imtest.png')
imtest = plt.imread('imtest.png') # <- final format I would like to have

I was wondering if you had any suggestions with regards to how I can do this "properly".

I tried to use PIL but didn't manage to succeed.

evan54
  • 3,585
  • 5
  • 34
  • 61

1 Answers1

0

Ok I think I found a way:

import PIL

img = PIL.Image.new('RGB', (200, 100))
d = PIL.ImageDraw.Draw(img)
d.text((20, 20), 'H', fill=(255, 255, 255))
img = np.asarray(img)

If anyone comes up with a better answer let me know. I should say that I don't understand how PIL works very well, so this is from gathering code I found in other places.

http://matplotlib.org/users/image_tutorial.html

Python Imaging Library - Text rendering

Community
  • 1
  • 1
evan54
  • 3,585
  • 5
  • 34
  • 61