OpenCV provides functions not only for drawing shapes, but also for texts. cv2.putText() function is used for drawing texts.
Similarly, define a wrapper function draw_text():
1 def draw_text(image, text, org,
2 font_face=cv2.FONT_HERSHEY_COMPLEX,
3 font_scale=1,
4 color=(255,255,255),
5 thickness=1,
6 line_type=cv2.LINE_AA):
7 cv2.putText(image, text, org, font_face,
8 font_scale, color, thickness, line_type )
Then create a canvas, paint it with light gray color, then call draw_ text() function to draw the texts.
10 canvas = np.zeros((380, 480, 3), np.uint8)
11 canvas[:] = 235,235,235
12 #Draw a text
13 draw_text(canvas, "Hello OpenCV", (50, 100),
14 color=(125, 0, 0),
15 font_scale=1.5,
16 thickness=2)
17 cv2.imshow("Hello OpenCV", canvas)
18 cv2.waitKey(0)
19 cv2.destroyAllWindows()
The result is shown as Figure shown below-
This version of OpenCV supports a limited set of fonts, below table shows the supported fonts.
0 comments:
Post a Comment