【发布时间】:2021-05-11 18:22:27
【问题描述】:
我一直在尝试将文本写入 80x80 16 位灰度图像,但在让它工作时遇到了一些问题。
我目前正在使用:
image = im[0]/255.0 #where im is just an np array of images (which are 80x80 np arrays)
# font
font = cv2.FONT_HERSHEY_SIMPLEX
# org
org = (40, 15)
# fontScale
fontScale = 0.3
# Blue color in BGR
color = (255.0)
# Line thickness of 2 px
thickness = 1
# Using cv2.putText() method
image = cv2.putText(image, 'Out:16', org, font, fontScale, color, thickness, cv2.LINE_AA)
# Displaying the image
cv2.imshow(window_name, image)
然而,文字不仅看起来很漂亮,而且占用了大量空间(我不能再往下看,否则它不清晰),除了文字是白色的,图像变得全黑。
是否有更好的方法将文本写入低分辨率图像(使文本更小)?为什么图像会变成全黑?
编辑:
我尝试使用 ImageDraw(),结果都是灰色的
from PIL import Image, ImageFont, ImageDraw
# creating a image object
image = Image.fromarray(im[0]/255.0)
draw = ImageDraw.Draw(image)
# specified font size
font = ImageFont.truetype('./arial.ttf', 10)
text = 'fyp:16'
# drawing text size
draw.text((5, 5), text, font = font, align ="left")
【问题讨论】:
-
如果您的 OpenCV 构建是使用 Qt 作为 HighGUI 后端构建的,那么有使用 Qt 绘制文本的功能:docs.opencv.org/4.5.2/dc/d46/group__highgui__qt.html |否则请查看 PIL/Pillow 中的
ImageDraw和ImageFont。我在一个在 32x32 像素图标上添加小标签的脚本中使用它。 -
@DanMašek 检查我的编辑
-
输入图像
im[0]是什么?数据类型、值的范围,可能会附上一个可用于重现此的示例。此外,附上实际输出——这看起来像是使用 matplotlib 放大的可视化?