简体   繁体   中英

how to print a text to a frame in opencv-python

I am using opencv-python for my face detection experiment. I am also to do a cv.drawRect function. I tried with cv.putText but it is not supported in python. Is there other functions other than cv.putText which can write text to an image frame

Got it. In the older version of python-opencv use PutText instead of putText

font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 3, 8) #Creates a font
x = 10 #position of text
y = 20 #position of text
cv.PutText(image,"Hello World!!!", (x,y),font, 255) #Draw the text

In case someone is interested to the new version of opencv-python:

import cv2

font = cv2.FONT_HERSHEY_DUPLEX
color = (255, 0, 0) # red
fontsize = 255
text = "test"
position = (10, 10)

cv2.putText(image, text, position, font, fontsize, color=color)

The available default fonts are defined in modules/imgproc/include/opencv2/imgproc.hpp header file:

enum HersheyFonts {
    FONT_HERSHEY_SIMPLEX        = 0, //!< normal size sans-serif font
    FONT_HERSHEY_PLAIN          = 1, //!< small size sans-serif font
    FONT_HERSHEY_DUPLEX         = 2, //!< normal size sans-serif font (more complex than FONT_HERSHEY_SIMPLEX)
    FONT_HERSHEY_COMPLEX        = 3, //!< normal size serif font
    FONT_HERSHEY_TRIPLEX        = 4, //!< normal size serif font (more complex than FONT_HERSHEY_COMPLEX)
    FONT_HERSHEY_COMPLEX_SMALL  = 5, //!< smaller version of FONT_HERSHEY_COMPLEX
    FONT_HERSHEY_SCRIPT_SIMPLEX = 6, //!< hand-writing style font
    FONT_HERSHEY_SCRIPT_COMPLEX = 7, //!< more complex variant of FONT_HERSHEY_SCRIPT_SIMPLEX
    FONT_ITALIC                 = 16 //!< flag for italic font
};

视觉字体示例

(Image source: Code Yarns )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM