1

I try to use python OpenCV (Windows 10, Python 3.6) to write title in image, when the text is English it works, but when I use Chinese title, it was out of order.

Below is my code:

import cv2

cap = cv2.VideoCapture(0)  # open webcam

while True:
    ret, frame = cap.read()  # 读Read the camera picture
    cv2.imshow('中文', frame)  # reveal 
    if cv2.waitKey(1) & 0xFF == ord('q'):  # 
        break

cap.release()  
cv2.destroyAllWindows()  

When title = "English" # just work, below is the output image:

enter image description here

When title = "中文" # doesn't work, below is the output image:

enter image description here

What I have tried adding encode('utf-8') or gbk to the title and then decoding it with errors='ignore', but it doesn't work, the title is still garbled.

I currently know that PIL can solve the problem of Chinese garbled characters in cv2.imshow's put text from How to draw Chinese text on the image using cv2.putTextcorrectly?, but I don't know how to solve the title problem, and i think they are different.

Markus
  • 5,976
  • 5
  • 6
  • 21
郑远程
  • 44
  • 5
  • You can use matplotlib, which is able to create windows with utf8 title. Example code: https://stackoverflow.com/a/44604435/18667225 – Markus Apr 27 '23 at 12:53

0 Answers0