MCPcopy Create free account
hub / github.com/PyImageSearch/imutils / put_text

Function put_text

imutils/text.py:4–52  ·  view source on GitHub ↗

Utility for drawing text with line breaks :param img: Image. :param text: Text string to be drawn. :param org: Bottom-left corner of the first line of the text string in the image. :param font_face: Font type. One of FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN, FONT_HERSHEY_DUPLEX,

(img, text, org, font_face, font_scale, color, thickness=1, line_type=8, bottom_left_origin=False)

Source from the content-addressed store, hash-verified

2
3
4def put_text(img, text, org, font_face, font_scale, color, thickness=1, line_type=8, bottom_left_origin=False):
5 """Utility for drawing text with line breaks
6
7 :param img: Image.
8 :param text: Text string to be drawn.
9 :param org: Bottom-left corner of the first line of the text string in the image.
10 :param font_face: Font type. One of FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN, FONT_HERSHEY_DUPLEX,
11 FONT_HERSHEY_COMPLEX, FONT_HERSHEY_TRIPLEX, FONT_HERSHEY_COMPLEX_SMALL,
12 FONT_HERSHEY_SCRIPT_SIMPLEX, or FONT_HERSHEY_SCRIPT_COMPLEX, where each of the font ID’s
13 can be combined with FONT_ITALIC to get the slanted letters.
14 :param font_scale: Font scale factor that is multiplied by the font-specific base size.
15 :param color: Text color.
16 :param thickness: Thickness of the lines used to draw a text.
17 :param line_type: Line type. See the line for details.
18 :param bottom_left_origin: When true, the image data origin is at the bottom-left corner.
19 Otherwise, it is at the top-left corner.
20 :return: None; image is modified in place
21 """
22 # Break out drawing coords
23 x, y = org
24
25 # Break text into list of text lines
26 text_lines = text.split('\n')
27
28 # Get height of text lines in pixels (height of all lines is the same)
29 _, line_height = cv2.getTextSize('', font_face, font_scale, thickness)[0]
30 # Set distance between lines in pixels
31 line_gap = line_height // 3
32
33 for i, text_line in enumerate(text_lines):
34 # Find total size of text block before this line
35 line_y_adjustment = i * (line_gap + line_height)
36
37 # Move text down from original line based on line number
38 if not bottom_left_origin:
39 line_y = y + line_y_adjustment
40 else:
41 line_y = y - line_y_adjustment
42
43 # Draw text
44 cv2.putText(img,
45 text=text_lines[i],
46 org=(x, line_y),
47 fontFace=font_face,
48 fontScale=font_scale,
49 color=color,
50 thickness=thickness,
51 lineType=line_type,
52 bottomLeftOrigin=bottom_left_origin)
53
54
55def put_centered_text(img, text, font_face, font_scale, color, thickness=1, line_type=8):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…