Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw Chinese text on the image using `cv2.putText`correctly? (Python+OpenCV)

Tags:

I use python cv2(window10, python2.7) to write text in image, when the text is English it works, but when I use Chinese text it write messy code in the image.

Below is my code:

# coding=utf-8 import cv2 import numpy as np  text = "Hello world"   # just work # text = "内容理解团队"  # messy text in the image  cv2.putText(img, text,             cord,             font,             fontScale,             fontColor,             lineType)  # Display the image cv2.imshow("img", img)  cv2.waitKey(0) cv2.destroyAllWindows() 

When text = "Hello world" # just work, below is the output image:

enter image description here

When text = "内容理解团队" # Chinese text, draw messy text in the image, below is the output image:

enter image description here

What's wrong? Does opencv putText don't support other language text?

like image 208
Jayhello Avatar asked Jun 14 '18 09:06

Jayhello


People also ask

How do I show text in cv2?

Step 1: Import cv2 Step 2: Define the parameters for the puttext( ) function. Step 3: Pass the parameters in to the puttext() function. Step 4: Display the image.

Is OpenCV Python and cv2 same?

cv2 is the module import name for opencv-python, "Unofficial pre-built CPU-only OpenCV packages for Python". The traditional OpenCV has many complicated steps involving building the module from scratch, which is unnecessary. I would recommend remaining with the opencv-python library.

What is the difference between OpenCV and cv2?

cv2 (old interface in old OpenCV versions was named as cv ) is the name that OpenCV developers chose when they created the binding generators. This is kept as the import name to be consistent with different kind of tutorials around the internet.


2 Answers

The cv2.putText don't support no-ascii char in my knowledge. Try to use PIL to draw NO-ASCII(such Chinese) on the image.

import numpy as np from PIL import ImageFont, ImageDraw, Image import cv2 import time  ## Make canvas and set the color img = np.zeros((200,400,3),np.uint8) b,g,r,a = 0,255,0,0  ## Use cv2.FONT_HERSHEY_XXX to write English. text = time.strftime("%Y/%m/%d %H:%M:%S %Z", time.localtime())  cv2.putText(img,  text, (50,50), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (b,g,r), 1, cv2.LINE_AA)   ## Use simsum.ttc to write Chinese. fontpath = "./simsun.ttc" # <== 这里是宋体路径  font = ImageFont.truetype(fontpath, 32) img_pil = Image.fromarray(img) draw = ImageDraw.Draw(img_pil) draw.text((50, 80),  "端午节就要到了。。。", font = font, fill = (b, g, r, a)) img = np.array(img_pil)  cv2.putText(img,  "--- by Silencer", (200,150), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (b,g,r), 1, cv2.LINE_AA)   ## Display  cv2.imshow("res", img);cv2.waitKey();cv2.destroyAllWindows() #cv2.imwrite("res.png", img) 

enter image description here


Refer to my another answer:

Load TrueType Font to OpenCV

like image 158
Kinght 金 Avatar answered Sep 17 '22 13:09

Kinght 金


According to this opencv forum, putText is only able to support a small ascii subset of characters and does not support unicode characters which are other symboles like chinese and arabic characters.

However, you can try to use PIL instead and follow the answer posted here and see if it works out for you.

like image 38
TMK Avatar answered Sep 17 '22 13:09

TMK