Write text annotation on image using Python Matplotlib.pyplot


The result is:

Write text annotation on image using Python Matplotlib.pyplot result

This page shows how to write texts and put plots on the image using Python and Matplotlib.pyplot. The Figures can be exported as vector image such as pdf and svg using plt.savefig. It is impossible if you use save function of PIL; Python Image Library.
The original data is:

Write text annotation on image using Python Matplotlib.pyplot original


In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
In [2]:
img = Image.open("./text_img.jpg")
In [3]:
plt.figure(figsize=(3.5,2.5))
plt.imshow(img)

plt.scatter([50],[30],s=100,facecolors="none",edgecolors="orange",lw=5)
plt.text(50+5,30,"Wow",color="orange",
         fontdict={"fontsize": 30,"fontweight":'bold',"ha":"left","va":"center"})
plt.axes().spines['top'].set_visible(False)
plt.axes().spines['left'].set_visible(False)
plt.axes().spines['bottom'].set_visible(False)
plt.axes().spines['right'].set_visible(False)
plt.xticks([])
plt.yticks([])
plt.savefig("./text_img_with_str",bbox_inches="tight",pad_inches=0.0)
In [4]:
# For calculate and process, 
# img = np.array(img)
# is easy way to do.