47 lines
938 B
Python
Executable File
47 lines
938 B
Python
Executable File
# Python program to explain cv2.putText() method
|
|
|
|
# importing cv2
|
|
import cv2
|
|
|
|
# text to display
|
|
text = 'OpenCV / press q to quit'
|
|
|
|
# path
|
|
path = r'./test_image.jpg'
|
|
|
|
# Reading an image in default mode
|
|
image = cv2.imread(path)
|
|
|
|
# Window name in which image is displayed
|
|
window_name = 'Image'
|
|
|
|
# font
|
|
font = cv2.FONT_HERSHEY_SIMPLEX
|
|
|
|
# org
|
|
org = (50, 50)
|
|
|
|
# fontScale
|
|
fontScale = 1
|
|
|
|
# Blue color in BGR
|
|
color = (255, 0, 0)
|
|
|
|
# Line thickness of 2 px
|
|
thickness = 2
|
|
|
|
# Using cv2.putText() method
|
|
image = cv2.putText(image, text, org, font,
|
|
fontScale, color, thickness, cv2.LINE_AA)
|
|
|
|
# you can also get the size of the text if needed to fix it in a box
|
|
text_width, text_height = cv2.getTextSize(text, font, fontScale, thickness)[0]
|
|
print(f"this text has size {text_width} {text_height}")
|
|
|
|
# Displaying the image
|
|
while True:
|
|
cv2.imshow(window_name, image)
|
|
key = cv2.waitKey(1)
|
|
if key == ord("q"):
|
|
break
|