![]() Hope you found this quick tutorial on writing text on images in OpenCV quite helpful. Also Read – Learn to Draw Rectangle in OpenCV Python using cv2.rectangle() with Examples.In the below example, we have created a black color rectangle and have written the text inside it by calculating the various coordinates.Ĭv2.rectangle(img3, (x,x), (x + w, y + h), (0,0,0), -1)Ĭv2.putText(img=img3, text="CAT",org=(x + int(w/10),y + int(h/1.5)), fontFace=cv2.FONT_HERSHEY_DUPLEX, fontScale=4, color=(255,0,0), thickness=7) We can create a small rectangle of the desired color on the image and put text on the rectangle. But there is a workaround for achieving this effect. OpenCV cv2.putText() does not have any built-in capabilities to have a background color for the text. In the below example, we split the text based on the newline character and then write each of the lines on the image in a loop.įor i, line in enumerate(text.split('\n')):Ĭv2.putText(img=img1, text=line, org=(150, y), fontFace=cv2.FONT_HERSHEY_SCRIPT_COMPLEX, fontScale=4, color=(255,255,0),Įxample – 3: Put Text with Background Color on Image in cv2.putText() We have to use certain workarounds to achieve this. OpenCV putText() function does not support writing text on multiple lines out of the box. In this first example, we will write a simple text on the blank image whose color is green, font is FONT_HERSHEY_TRIPLEX, font scale is 3 and thickness is 3.Ĭv2.putText(img=img, text='Hello', org=(150, 250), fontFace=cv2.FONT_HERSHEY_TRIPLEX, fontScale=3, color=(0, 255, 0),thickness=3)Įxample – 2: Put Text on Multiple Lines in cv2.putText() In : sample_img = generate_empty_image()Įxample – 1: Put Text on Image with cv2.putText() Let us see this function in action by calling it below. We will use this blank image to write text onto it. ![]() We will write a utility function that helps to create an empty image of the size 512×512 and 3 color channels. %matplotlib inline Utility Function to Create Empty Image ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |