Draw a text on an image with OpenCV

In OpenCV, use the cv2.putText() function to draw text on an image. First, we will read the image using the imread() function in OpenCV.

Let us now see the example:

The following is the output. We have written a text ā€œSubscribeā€ on the image:

Draw a text on an image with OpenCV
Explanation:

This Python script uses the OpenCV library to load, resize, and add text to an image:

  • It begins by importing the OpenCV library and setting a specific font style using cv2.FONT_HERSHEY_SIMPLEX.
  • The script then reads an image from the specified file path and loads it in color.
  • The image is resized to dimensions of 600×600 pixels using the cv2.resize() function.
  • After resizing, the script places the text “Subscribe” on the image at coordinates (230, 510) using the cv2.putText() function. The arguments for cv2.putText() include the image, the text, the coordinates, the font, the font scale (1), the color (red), and the thickness (3 pixels).
  • Finally, the script displays the resized image with the added text in a window titled “Image Resized:” using cv2.imshow(), waits for a key press with cv2.waitKey(0), and closes all OpenCV windows with cv2.destroyAllWindows().
  • This process allows users to view the modified image with the added text and close the display window at their convenience.

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel:Ā Join Now


Read More:

Draw a line on an image with OpenCV
Set a Grayscale filter with OpenCV
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading