Tuesday, 21 November 2023

Adding Text to Images with OpenCV

 

‘cv2.putText’ method is used to write text on the image.

 

Signature

cv2.putText(image, text, org, fontFace, fontScale, color, thickness, lineType, bottomLeftOrigin)

 

Following table summarizes the parameters of putText method.

 

Parameter

Description

image

Specifies the image on which you want to draw the text

text

Represents the text string that we want to draw in image

org

It is a tuple (x, y), specifies the bottom-left corner of the text string in the image.

fontFace

Specifies the font type to be used for the text.

fontScale

Specifies the scale factor that is multiplied by the font-specific base size.

color

Text color, color is specified in BGR (Blue, Green, Red) format.

thickness

It is an integer, specifies the thickness of the text lines.

lineType

Specifies the type of line used to draw the text.

Ex: cv2.LINE_8

bottomLeftOrigin

It is a boolen flag. When it is set to True, then the org parameter is specified with respect to the bottom-left corner of the text bounding box.

When it is set to True, then the org parameter is specified with respect to the top-left corner corner of the text bounding box.

 

 

put_text_on_image.py

import cv2
import numpy as np

# Create a black image
image = np.zeros((400, 500, 3), dtype=np.uint8)
image[:] = [255, 255, 255]

# Define the text to be added
text = "Hello, World!!!!"

# Draw the text
cv2.putText(image, text, org=(50, 150), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1.5, color=(255, 0, 0), thickness=2)
cv2.putText(image, text, org=(50, 200), fontFace=cv2.FONT_HERSHEY_DUPLEX,  fontScale=1, color=(0, 0, 255), thickness=2)

# Display the image
cv2.imshow("Text Example", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

 


  

Previous                                                    Next                                                    Home

No comments:

Post a Comment