OpenCV module for drawing UTF-8 strings with freetype2
Thankfully a new module has been added to OpenCV that makes rendering text using the fonts supported by FreeType (including TrueType) onto images very easy. This page documents this module. In Python that means there is no need to use Cairo or PIL. The following code shows how this modules can be used in Python.
import cv2
import numpy as np
img = np.zeros((100, 300, 3), dtype=np.uint8)
ft = cv2.freetype.createFreeType2()
ft.loadFontData(fontFileName='Ubuntu-R.ttf',
id=0)
ft.putText(img=img,
text='Quick Fox',
org=(15, 70),
fontHeight=60,
color=(255, 255, 255),
thickness=-1,
line_type=cv2.LINE_AA,
bottomLeftOrigin=True)
cv2.imwrite('image.png', img)
The PNG image should like this: