擅长:python、mysql、java
<pre><code>import textwrap
def get_text(img, text):
sh = img.shape
txt = np.ones(shape=sh)
fontface = cv2.FONT_HERSHEY_SIMPLEX
fontscale = 1
thickness = 2
color = (0, 0, 0)
orig = (10, 100)
linetype = cv2.LINE_AA
wrapped_text = textwrap.wrap(text, width=35)
x, y = 10, 40
font_size = 1
font_thickness = 2
i = 0
for line in wrapped_text:
textsize = cv2.getTextSize(line, font, font_size, font_thickness)[0]
gap = textsize[1] + 10
y = int((img.shape[0] + textsize[1]) / 2) + i * gap
x = int((img.shape[1] - textsize[0]) / 2)
cv2.putText(img, line, (x, y), font,
font_size,
(0,0,0),
font_thickness,
lineType = cv2.LINE_AA)
txt = txt.astype("uint8")
return txt
</code></pre>
<p>尝试这个,可能需要一些调整,但是想法是使用<code>textwrap.wrap(text, width=35)</code>。你知道吗</p>