cv3.draw.text

cv3.draw.text(img, text_str, x=0.5, y=0.5, font=None, scale=None, flip=False, rel=None, color=None, t=None, line_type=None, copy=False)[source]

Draw text on an image.

Parameters:
  • img (numpy.ndarray) – Input image to draw on.

  • text_str (str) – Text string to be drawn.

  • x (int or float, optional) – X-coordinate of the bottom-left corner of the text. Defaults to 0.5 (center).

  • y (int or float, optional) – Y-coordinate of the bottom-left corner of the text. Defaults to 0.5 (center).

  • font (int or str, optional) – Font type. Can be an OpenCV font flag or string. Available string options: ‘simplex’, ‘plain’, ‘duplex’, ‘complex’, ‘triplex’, ‘complex_small’, ‘script_simplex’, ‘script_complex’, ‘italic’. Defaults to opt.FONT.

  • scale (float, optional) – Font scale factor that is multiplied by the font-specific base size. Defaults to opt.SCALE.

  • flip (bool, optional) – If True, the text is rendered upside down. Defaults to False.

  • rel (bool, optional) – Whether to use relative coordinates. Defaults to None.

  • color – Color of the text (default: opt.COLOR).

  • t – Thickness of the text strokes (default: opt.THICKNESS).

  • line_type – Type of line for drawing (default: opt.LINE_TYPE).

  • copy (bool) – Whether to copy the image before drawing (default: False).

Returns:

Image with the text drawn on it.

Return type:

numpy.ndarray

Note

Relative coordinates are in the range [0, 1] where (0, 0) is the top-left and (1, 1) is the bottom-right of the image.

The default position (x=0.5, y=0.5) places the text near the center of the image.

Example

>>> import cv3
>>> img = cv3.zeros(100, 100, 3)
>>> # Draw text at the center
>>> img = cv3.text(img, 'Hello', color='red')
>>> # Draw text with custom position and font
>>> img = cv3.text(img, 'World', x=10, y=50, font='complex', scale=1.2, color='blue')
>>> # Draw flipped text
>>> img = cv3.text(img, 'Flipped', x=50, y=80, flip=True, color='green')