Module 'cv2' has no attribute 'freetype'

Here is a quick sample code

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 above example works fine if I build OpenCV from source, however, if I use the pip install opencv-contrib-python or pip install opencv-python, I get error 'module ‘cv2’ has no attribute ‘freetype’ ’

Any recommendations? Or do I just need to build OpenCV?

Thanks!

I think the answer is here
licenses problem

the FreeType License (FTL) is the most commonly used one. It is a BSD-style license with a credit clause and thus compatible with the GNU Public License (GPL) version 3, but not with the GPL version 2.

it’s actually here:

the +contrib binary package does contain some stuff that may have licensing issues, but not everything.

usually it’s a matter of how difficult those libraries (freetype, harfbuzz, …) make it to build against them.