Python OpenCV: How to draw text on an image

Introduction

In this tutorial we will learn how to draw text on an image, using Python and OpenCV.

The code we will analyze was tested on Windows 8.1, with version 4.1.2 of OpenCV. The Python version used was 3.7.2.

How to draw text on the image

We will start by importing the cv2 module, which will expose to us the function we need to draw text on an image.

import cv2

After that, we will load an image from the file system with a call to the imread function. As input, we pass the path to the image we want to use. When testing the code, don’t forget to adapt the code to point to an image in your machine.

img = cv2.imread('C:/Users/N/Desktop/monument.png')

Now, to draw text on the image, we will make use of the putText function. It receives the following parameters:

  • Image that will be used to write the text. We will pass the image we have just read;
  • Text to be drawn in the image, as a string. We will pass a testing string;
  • A tuple containing the pixel coordinates (x and y) of the bottom left corner where to draw the text. We will draw it at the top left corner of the image by using the coordinates x = 0 and y = 30.
  • The font, from the cv2 module. The list of possible values can be found here. In this example, we will be using FONT_HERSHEY_SIMPLEX;
  • The scale of the text. We will pass the value 1.0 to keep the font base size;
  • A tuple containing the BGR values for the color of the text. We will set our text to black, which means setting B=0, G=0 and R=0;
  • The thickness of the lines of the text. We will set it to 4. This is an optional parameter that, when not specified, defaults to 1.

Note that the putText function changes the image we pass as input. So, if we need to preserve the original image for some reason, we can simply copy it and write the text on the copy. This post explains how to copy an image.

cv2.putText(img, "My text", (0, 30), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 0), 4)

Now that we have drawn the text on the image, we will display it in a window. This is done simply by calling the imshow function, passing as first input the name of the window and as second our image.

cv2.imshow('image', img)

To finalize, we will wait for the user input to destroy the window and end our program.

cv2.waitKey(0)
cv2.destroyAllWindows()

The complete code can be seen below.

import cv2

img = cv2.imread('C:/Users/N/Desktop/monument.png')

cv2.putText(img, "My text", (0, 30), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 0), 4)

cv2.imshow('image', img)

cv2.waitKey(0)
cv2.destroyAllWindows()

To test the previous script, you simply need to run it in a tool of your choice. I’ll be using PyCharm, a Python IDE.

Upon running the code, you should obtain a result similar to the one shown in figure 1. As we can check, we obtain an image with the text written in the top left corner, as expected.

Output of the OpenCV program, showing the image with the text drawn.
Figure 1 – Output of the program, showing the image with the text drawn.

A more complex example

Since we already covered the basics, we will now analyze a more complex example where we will add a slider to the image to control the thickness of the text. The procedure to add a slider to a window was already covered here.

Like before, we will start by importing the cv2 module. Then, we will read an image from the file system and display it in a window. We will keep the window name in a variable since we will need it to create the slider.

Note that we are not going to write any text right away. This means that, when we run the program, it won’t show any text until we move the slider for the first time.

import cv2

img = cv2.imread('C:/Users/N/Desktop/monument.png')

windowName = 'image'

cv2.imshow(windowName, img)

Then we will add the slider to our image by calling the createTrackbar function. As input we pass the following values:

  • The name of the slider. We will call it “Thickness”;
  • The name of the window where to add the slider. We will pass the variable that holds the name of the window we created before;
  • The initial value to set the slider. We will set it to the beginning of the scale, which corresponds to zero;
  • The maximum value of the slider scale. We will use a value of 10;
  • The callback function that will be executed when the slider value changes. We will call this function on_change_thickness and analyze its implementation below.
cv2.createTrackbar('Thickness', windowName, 0, 10, on_change_thickness)

After this, we will wait for the user input. When it happens, we will destroy the window and finish the execution of our code.

cv2.waitKey(0)
cv2.destroyAllWindows()

To finish, we will analyze the implementation of the on_change_thickness callback function. Note that it will receive as input the current value of the slider.

def on_change_thickness(val):
    # implementation

Like already commented on the previous section, the putText function alters the image. So, if we write with different thicknesses in the same place, we will get overlapped text. To avoid this, we will make a copy of the original image and write the text with the new thickness on this copy.

imageCopy = img.copy()

cv2.putText(imageCopy, "My text", (0, 100), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 0, 0), val)

Then, we will display the image in the same window we have already created.

cv2.imshow(windowName, imageCopy)

The whole callback function can be seen below.

def on_change_thickness(val):
    imageCopy = img.copy()

    cv2.putText(imageCopy, "My text", (0, 100), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 0, 0), val)
    cv2.imshow(windowName, imageCopy)

The final Python script can be seen in the snippet below.

import cv2


def on_change_thickness(val):
    imageCopy = img.copy()

    cv2.putText(imageCopy, "My text", (0, 100), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 0, 0), val)
    cv2.imshow(windowName, imageCopy)


img = cv2.imread('C:/Users/N/Desktop/monument.png')

windowName = 'image'

cv2.imshow(windowName, img)

cv2.createTrackbar('Thickness', windowName, 0, 10, on_change_thickness)


cv2.waitKey(0)
cv2.destroyAllWindows()

Like before, simply run the code and check the result. You should get an output like the one shown in figure 2.

Adding a slider to the window to change the thickness of the text.
Figure 2 – Adding a slider to the window to change the thickness of the text.

Suggested OpenCV readings

Leave a Reply

%d bloggers like this: