10

I am adding text in OpenCV like this...

import numpy as np
import cv2
font = cv2.FONT_HERSHEY_SIMPLEX
# Create a black image
img = np.zeros((512,512,3), np.uint8)

cv2.putText(img,'Hack Projects',(10,500), font, 1,(255,255,255),2)
#Display the image
cv2.imshow("img",img)

cv2.waitKey(0)

This works, but the text is not very good quality. Anybody know what I am doing wrong?

fightstarr20
  • 11,682
  • 40
  • 154
  • 278
  • it is natural - text is always so ugly. – furas Aug 11 '19 at 15:24
  • 1
    you can put image with text or use `pillow` to put text - [Load TrueType Font to OpenCV](https://stackoverflow.com/questions/37191008/load-truetype-font-to-opencv) – furas Aug 11 '19 at 15:31

1 Answers1

24

As noted in the tutorial, text in OpenCV looks better if you add lineType = cv2.LINE_AA to give you anti-aliased lines instead of the default cv.LINE_8.

Changing your code to:

cv2.putText(img,'Hack Projects',(10,500), font, 1,(255,255,255),2, cv2.LINE_AA)

changes the image from this:

text with no anti-aliasing

to this:

text with anti-aliasing

beaker
  • 16,331
  • 3
  • 32
  • 49