8

The above answer doesn't solve my problem.

I am using cv2.putText() to put text over a video.

This works as expected, but I am attempting to use a different font (not available in OpenCV).

I understand that OpenCV is limited to the cv2.FONT_HERSHEY fonts, so I am using PIL with OpenCV to achieve this.

I used this method with images and that experiment was successful. But I am failing when I try something similar on a video.

import cv2
from PIL import ImageFont, ImageDraw, Image

camera = cv2.VideoCapture('some_video.wmv')
while cv2.waitKey(30) < 0:
    rv, frame = camera.read()
    if rv:
        font = ImageFont.truetype("calibrii.ttf", 80)
        cv2.putText(frame, 'Hello World!', (600, 600), font, 2.8, 255)
        cv2.imshow('Video', frame)

I have the "calibrii.ttf" in the same directory and as I mentioned, this approach worked with images.

Here is the error:

cv2.putText(frame, 'Hello World!', (600, 600), font, 2.8, 255)
TypeError: an integer is required (got type FreeTypeFont)
Community
  • 1
  • 1
Joe T. Boka
  • 6,554
  • 6
  • 29
  • 48
  • 3
    Possible duplicate of [Load TrueType Font to OpenCV](https://stackoverflow.com/questions/37191008/load-truetype-font-to-opencv) – Kinght 金 Jun 15 '18 at 06:35
  • 1
    @Silencer I don't understand how you could think that this is a duplicate question. I get it that in the answer you're linking to is your own answer; but your answer is clearly for images not video. I explained in my question that I have the solution for images, but my question is how to apply a similar PIL solution to video. Do you have a solution to my question? – Joe T. Boka Jun 15 '18 at 06:52
  • 2
    @zindarod Thanks for your comment. I literally just started learning OpenCV last week for the first time. That said, I know a video is sequence of images...haha. I just built some videos from images I created in OpenCV. It's a lot of fun, but I would like to use the already existing `cv2.putText` to put the text directly to the video, which works great!! Just, the HERSHEY font happens to be ugly. So, I am trying to avoid doing this the hard way i.e. image by image. – Joe T. Boka Jun 15 '18 at 09:38
  • This may be useful... https://stackoverflow.com/a/53697181/2836621 – Mark Setchell Jul 02 '19 at 17:12

1 Answers1

10

You can use OpenCV's freetype module to do so, no need to use PIL.

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 output of this screen is here.

fireant
  • 14,080
  • 4
  • 39
  • 48
  • 4
    Thanks so much for helping me with this. At first try, I am getting the `AttributeError: module 'cv2' has no attribute 'freetype'`. I am using `opencv 3.4.1` with `python 3.5.2`. Any suggestions? Thanks again. – Joe T. Boka Jun 19 '18 at 06:18
  • 1
    Probably need this `pip install opencv-contrib-python` – fireant Jun 19 '18 at 06:27
  • 6
    I created an new environment in Conda and installed `opencv-contrib-python`. I can `import cv2`, so it works, but I still get the same `AttributeError`. I am so excited about this module, I hope I can make it work. What could be the problem? – Joe T. Boka Jun 19 '18 at 06:44
  • 3
    According to this: https://stackoverflow.com/questions/47726854/error-module-object-has-no-attribute-freetype, freetype does not work with Python – Kat Sep 17 '19 at 20:34