python opencv根据图像大小调整cv2.putText的文本大小

2024-09-28 17:21:55 发布

您现在位置:Python中文网/ 问答频道 /正文

    fontScale = 1
    fontThickness = 1

    # make sure font thickness is an integer, if not, the OpenCV functions that use this may crash
    fontThickness = int(fontThickness)

    upperLeftTextOriginX = int(imageWidth * 0.05)
    upperLeftTextOriginY = int(imageHeight * 0.05)

    textSize, baseline = cv2.getTextSize(resultText, fontFace, fontScale, fontThickness)
    textSizeWidth, textSizeHeight = textSize

    # calculate the lower left origin of the text area based on the text area center, width, and height
    lowerLeftTextOriginX = upperLeftTextOriginX
    lowerLeftTextOriginY = upperLeftTextOriginY + textSizeHeight

    # write the text on the image
    cv2.putText(openCVImage, resultText, (lowerLeftTextOriginX, lowerLeftTextOriginY), fontFace, fontScale, Color,
                fontThickness)

似乎fontScale没有根据图像的宽度和高度缩放文本,因为对于不同大小的图像,文本的大小几乎相同。那么,我如何根据图像大小调整文本大小,以便所有文本都能适合图像?在


Tags: thetext图像文本areacv2intfontface
2条回答

因为这起作用了!在

scale = 1 # this value can be from 0 to 1 (0,1] to change the size of the text relative to the image
fontScale = min(imageWidth,imageHeight)/(25/scale)

请记住,字体类型会影响25常量

如果将fontScale = 1用于大小约为1000 x 1000的图像,则此代码应正确缩放字体。在

fontScale = (imageWidth * imageHeight) / (1000 * 1000) # Would work best for almost square images

如果你仍然有任何问题,请发表意见。在

相关问题 更多 >