3

I want to apply some text to an image using python cv2 module. I have found two cv2 functions that can do it cv2.putText and cv2.addText. I would like to know the pro & cons for each.

Thank you

cnemri
  • 454
  • 4
  • 14
  • Can you link something about ```addText``` cos I am hearing first time and not able to find any document about it – Yunus Temurlenk Jun 10 '20 at 11:23
  • Apparently the differences include `addText` [can use system fonts](https://stackoverflow.com/q/11917124/774575), `putText` might be limited to Hershey via the font enumeration members. – mins Feb 25 '21 at 15:46

2 Answers2

3

Just like Yunus from the first comment I did not know the addText function yet. And had to do some research first.

In the c++ source code the two functions are implemented in different modules. The putText function in imgproc and the addText function in the highgui module. The latter is as far as I know intended for the easy creation of GUIs. I guess that the addText function should be used in this GUI context.

This is the only documentation I could find about addText in python: https://kite.com/python/docs/cv2.addText The call looks identical so far. Nevertheless I get the following error when executing the following program:

img = cv2.imread("img url")

t = time.time()
cv2.putText(img, "Hello", (20,20), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0))
print(str(time.time() - t))
t = time.time()
cv2.addText(img, "World", (50,20), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0))
print(str(time.time() - t))

cv2.imshow("test", img)
cv2.waitKey(0)

Error:

Traceback (most recent call last):
   File ".../test.py", line 20, in <module>
      main()
   File ".../test.py", line 12, in main
      cv2.addText(img, "World", (50,20), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0))
SystemError: <built-in function addText> returned NULL without setting an error

To put it in your pro, con list:

putText: + works + usualy used - nothing

addText: (+) might be usable in gui stuff - found no working documentation

S. Vogt
  • 375
  • 2
  • 9
  • Thank you very much. That's exactly what I am looking for. I did not know about it either. My IDE has intellisense feature and suggested me both. – cnemri Jun 10 '20 at 12:19
  • I would assume addText is not working due to the font. According to the documentation: > The function addText draws text on the image img using a specific font font (see example cv::fontQt ) Which hints at addText not using the cv2.HERSHEY... fonts, but something inspired by the underlying Qt. – simmue Jun 15 '23 at 13:48
2

Adding for completeness and eventually gather more information:

Actually cv2.AddText seems to be used in Qt context, if you compile OpenCV with Qt support. See this page for more API and usage.

But if you open a console and insert below commands to know about Python OpenCV support:

import cv2   # version 4.5.0 complied with Qt support
res = [f for f in dir(cv2) if 'Text' in f]
print(res)
['addText', 'getTextSize', 'putText', 'text_TextDetector', 'text_TextDetectorCNN', 'ximgproc_segmentation_SelectiveSearchSegmentationStrategyTexture']

We can see both addText and putText.

However if we search for "fontQt" function as in documentation (this page):

res = [f for f in dir(cv2) if 'fontQt' in f]   # no results for 'Qt' also
print(res)
[]

So it seems Qt font support should be available in OpenCV but there is no Python wrapper to it.

dpetrini
  • 1,169
  • 1
  • 12
  • 25