In OpenCV, sono supportati solo un sottoinsieme di font Hershey.
In opencv2/core.hpp
, è possibile trovare questo enum HersheyFonts.
//! Only a subset of Hershey fonts
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
};
È possibile provare PIL.ImageFont se si desidera utilizzare un carattere personalizzato.
Un esempio di base è presentato qui:
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, 100), "国庆节/中秋节 快乐!", font = font, fill = (b, g, r, a))
img = np.array(img_pil)
## Display
cv2.imshow("res", img);cv2.waitKey();cv2.destroyAllWindows()
cv2.imwrite("res.png", img)

No, con 'cv2.putText' è possibile utilizzare solo il piccolo sottoinsieme di font Hershey inclusi in OpenCV. Che dire dell'utilizzo del [supporto TrueType di Pillow] (http://pillow.readthedocs.io/en/2.8.1/reference/ImageFont.html)? Ci sono esempi su come gestire le conversioni di OpenCV <-> PIL qui su Stack Overflow. –