Python OpenCv: Write text on video

OpenCV is the huge open-source library for computer vision, machine learning, and image processing and now it plays a major role in real-time operation which is very important in today’s systems. By using it, one can process images and videos to identify objects, faces, or even the handwriting of a human.

cv2.putText() method inserts a text on the video frame at the desired position specified by the user. We can style the type of font and also it’s color and thickness.

Syntax : cv2.putText(frame, Text, org, font, color, thickness)

Parameters:
frame: current running frame of the video.
Text: The text string to be inserted.
org: bottom-left corner of the text string
font: the type of font to be used.
color: the colour of the font.
thickness: the thickness of the font

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to write
# text on video
  
  
import cv2
  
  
cap = cv2.VideoCapture('sample_vid.mp4')
  
while(True):
      
    # Capture frames in the video
    ret, frame = cap.read()
  
    # describe the type of font
    # to be used.
    font = cv2.FONT_HERSHEY_SIMPLEX
  
    # Use putText() method for
    # inserting text on video
    cv2.putText(frame, 
                'TEXT ON VIDEO'
                (50, 50), 
                font, 1
                (0, 255, 255), 
                2
                cv2.LINE_4)
  
    # Display the resulting frame
    cv2.imshow('video', frame)
  
    # creating 'q' as the quit 
    # button for the video
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
  
# release the cap object
cap.release()
# close all windows
cv2.destroyAllWindows()

chevron_right


Output:

python-write-to-video-opencv




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.