Life2Coding
How to Put Text on Image using OpenCV in Python

This post will be helpful in learning OpenCV using Python programming. Here I will show how to implement OpenCV functions and apply them in various aspects using some great examples. Then the output will be visualized along with the comparisons.

We will also discuss the basic of image processing and provide the detail explanation related to the OpenCV functions. We’re going to discuss how to put text on images.

Requirements:

First, you need to setup your Python Environment with OpenCV. You can easily do it by following Life2Coding’s tutorial on YouTube: Linking OpenCV with Python 3

Goals:

The goal is to make you understand how to put text on image using Python OpenCV.

Documentation:

rectangle()

img=cv.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]])
img=cv.rectangle(img, rec, color[, thickness[, lineType[, shift]]])
img=cv.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]])
img=cv.rectangle(img, rec, color[, thickness[, lineType[, shift]]])

Draws a simple, thick, or filled up-right rectangle.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
img Image.
pt1 Vertex of the rectangle.
pt2 Vertex of the rectangle opposite to pt1 .
color Rectangle color or brightness (grayscale image).
thickness Thickness of lines that make up the rectangle. Negative values, like FILLED, mean that the function has to draw a filled rectangle.
lineType Type of the line. See LineTypes
shift Number of fractional bits in the point coordinates.

putText()

img=cv.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])

Draws a text string.

Parameters
img Image.
text Text string to be drawn.
org Bottom-left corner of the text string in the image.
fontFace Font type, see HersheyFonts.
fontScale Font scale factor that is multiplied by the font-specific base size.
color Text color.
thickness Thickness of the lines used to draw a text.
lineType Line type. See LineTypes
bottomLeftOrigin When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner.

imshow()

None=cv.imshow(winname, mat)

Displays an image in the specified window.

Parameters
winname Name of the window.
mat Image to be shown.

waitKey()

retval=cv.waitKey([, delay])

Waits for a pressed key.

Parameters
delay Delay in milliseconds. 0 is the special value that means “forever”.

destroyAllWindows()

None=cv.destroyAllWindows()

Destroys all of the HighGUI windows.

Steps:

  • First we will create a image array using np.zeros()
  • We will define a rectangle around the text using cv2.rectangle()
  • After that we will put a text using cv2.putText()
  • Then display the image using cv2.imshow()
  • Wait for keyboard button press using cv2.waitKey()
  • Exit window and destroy all windows using cv2.destroyAllWindows()

Example Code:

import numpy as np
import cv2
#create a 512x512 black image
img=np.zeros((512,512,3),np.uint8)
# Draw a rectangle around the text
ori = cv2.rectangle(img,(10,180), (500,300), (0,0,255), 4)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(ori,'Life2Coding',(30,256), font, 2.5,(255,255,255),2,cv2.LINE_AA)
#now use a frame to show it just as displaying a image
cv2.imshow("Text",ori)
cv2.waitKey(0)           
cv2.destroyAllWindows()


Output:

5cb403f500ed4 How to Put Text on Image using OpenCV in Python
life2coding_icon [] How to Put Text on Image using OpenCV in Python

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.