<p>在这段代码中,我只是根据图像宽度将字符串分割成几个部分。你知道吗</p>
<pre><code> # Python program to explain cv2.putText() method
import cv2
import math
import textwrap
path = r'YOUR PATH OF IMAGE'
image = cv2.imread(path)
window_name = 'Image'
font = cv2.FONT_HERSHEY_SIMPLEX
zero= 5
one =50
org = (zero, one)
fontScale = 1
color = (255, 0, 0)
thickness = 2
imageHeight = image.shape[0]
imageWidth = image.shape[1]
print(f"width:",imageWidth)
sizeofnumpix=min(imageWidth,imageHeight)/(25/fontScale)
stringputt = 'YOUR STRING'
i=len(stringputt)
print(i)
if i*sizeofnumpix > imageWidth*2:
n=math.ceil(i*sizeofnumpix/(imageWidth*2))
part_size = math.ceil(i/n)
txt = textwrap.wrap(stringputt, part_size)
for l in txt:
image = cv2.putText(image, l, org, font,fontScale, color, thickness, cv2.LINE_AA)
zero= 5
one = one+math.ceil(sizeofnumpix)
org = (zero, one)
else:
image = cv2.putText(image, stringputt, org, font,fontScale, color, thickness, cv2.LINE_AA)
# Displaying the image
cv2.imwrite('IMGWITHTXT.png',image)
</code></pre>