Life2Coding
How to Draw or Write Different Style Texts on OpenCV Images

This post will be helpful in learning OpenCV using Python programming. Here I will show how to implement OpenCV functions and apply it in various aspects using some examples. Then the output will be shown with some comparisons as well.

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 3 with Python 3

Goals:

In this tutorial, I will show you how to draw different style text on images using OpenCV library and Python coding.

Documentation:

Python: cv2.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]]) → None

Draws a text string.

Parameters:          

  • img – Image.
  • text – Text string to be drawn.
  • org – Bottom-left corner of the text string in the image.
  • font – CvFont structure initialized using InitFont().
  • fontFace  – Font type. One of FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN, FONT_HERSHEY_DUPLEX, FONT_HERSHEY_COMPLEX, FONT_HERSHEY_TRIPLEX, FONT_HERSHEY_COMPLEX_SMALL, FONT_HERSHEY_SCRIPT_SIMPLEX, or FONT_HERSHEY_SCRIPT_COMPLEX, where each of the font ID’s can be combined with FONT_ITALIC to get the slanted letters.
  • fontScale – Font scale factor that is multiplied by the font-specific base size.
  • color – Text color (B,G,R format).
  • thickness – Thickness of the lines used to draw a text.
  • lineType – Line type. See the line for details.
  • bottomLeftOrigin – When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner.           

Steps:

  • Draw different style texts using cv2.putText()
  • Create window using cv2.namedWindow()
  • Display Image using cv2.imshow()
  • Save the output in an image file using cv2.imwrite()
  • Wait for keyboard button press using cv2.waitKey()
  • Exit window and destroy all windows using cv2.destroyAllWindows()

Example Code:

import cv2
import numpy as np

def  drawOpenCVText():
    # Create a black image
    img = np.zeros((640, 720, 3), np.uint8)
    #change the black image to white image by filling all values with 255
    img.fill(255)

    """                      
    Availble font lists
    FONT_HERSHEY_COMPLEX
    FONT_HERSHEY_COMPLEX_SMALL
    FONT_HERSHEY_DUPLEX
    FONT_HERSHEY_PLAIN
    FONT_HERSHEY_SCRIPT_COMPLEX
    FONT_HERSHEY_SCRIPT_SIMPLEX
    FONT_HERSHEY_SIMPLEX
    FONT_HERSHEY_TRIPLEX
    FONT_ITALIC
    """
    fontscale = 1.0
    # (B, G, R)
    color = (0, 0, 255)
    # select font
    fontface = cv2.FONT_HERSHEY_COMPLEX
    cv2.putText(img, "FONT_HERSHEY_COMPLEX", (25, 40), fontface, fontscale, color)
    fontface = cv2.FONT_HERSHEY_COMPLEX_SMALL
    cv2.putText(img, "FONT_HERSHEY_COMPLEX_SMALL", (25, 80), fontface, fontscale, color)
    fontface = cv2.FONT_HERSHEY_DUPLEX
    cv2.putText(img, "FONT_HERSHEY_DUPLEX", (25, 120), fontface, fontscale, color)
    fontface = cv2.FONT_HERSHEY_PLAIN
    cv2.putText(img, "FONT_HERSHEY_PLAIN", (25, 160), fontface, fontscale, color)
    fontface = cv2.FONT_HERSHEY_SCRIPT_COMPLEX
    cv2.putText(img, "FONT_HERSHEY_SCRIPT_COMPLEX", (25, 200), fontface, fontscale, color)
    fontface = cv2.FONT_HERSHEY_SCRIPT_SIMPLEX
    cv2.putText(img, "FONT_HERSHEY_SCRIPT_SIMPLEX", (25, 240), fontface, fontscale, color)
    fontface = cv2.FONT_HERSHEY_SIMPLEX
    cv2.putText(img, "FONT_HERSHEY_SIMPLEX", (25, 280), fontface, fontscale, color)
    fontface = cv2.FONT_HERSHEY_TRIPLEX
    cv2.putText(img, "FONT_HERSHEY_TRIPLEX", (25, 320), fontface, fontscale, color)
    fontface = cv2.FONT_ITALIC
    cv2.putText(img, "FONT_ITALIC", (25, 360), fontface, fontscale, color)

    cv2.namedWindow('putText Example')
    #display and save image

    cv2.imshow('putText Example', img)
    cv2.imwrite("output.jpg", img)

    cv2.waitKey(0)
    cv2.destroyAllWindows()

def main():
    drawOpenCVText()

if __name__ == '__main__':
    main()

Output:

1 How to Draw or Write Different Style Texts on OpenCV Images

life2coding_icon [] How to Draw or Write Different Style Texts on OpenCV Images

Leave a Reply

Your email address will not be published.

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