如何使用 opencv 将等宽字体插入图像中? [英] How can I insert Monospace fonts into an image with opencv?

查看:18
本文介绍了如何使用 opencv 将等宽字体插入图像中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我可以使用 openCV API (putText) 将一些 HERSHEY 字体文本插入到图像中.但似乎 openCV 不支持任何等宽字体.

我想知道如何在图像中插入一些等宽或固定间距的文本.

解决方案

您可以很容易地将 PIL/Pillow 用于该方面.OpenCV 图像是 numpy 数组,因此您可以使用以下命令从 OpenCV 图像制作枕头图像:

PilImage = Image.fromarray(OpenCVimage)

然后,您可以使用我的答案

<小时>

或者,您可以让函数自己生成一块画布,在上面写下您的文本,然后将其拼接到您想要的 OpenCV 图像中.沿着这些路线的一些东西 - 虽然我不知道你需要什么样的灵活性所以我没有参数化一切:

#!/usr/local/bin/python3从 PIL 导入 Image、ImageFont、ImageDraw、ImageColor将 numpy 导入为 np导入 cv2def GenerateText(size, fontsize, bg, fg, text):"""生成一块画布并在其上绘制文本"""canvas = Image.new('RGB', size, bg)# 获取绘图上下文绘制 = ImageDraw.Draw(画布)monospace = ImageFont.truetype("/Library/Fonts/Andale Mono.ttf",fontsize)draw.text((10, 10), text, fg, font=monospace)# 更改为 OpenCV 特性的 BGR 顺序返回 cv2.cvtColor(np.array(canvas), cv2.COLOR_RGB2BGR)# 用 OpenCV 打开图片im_o = cv2.imread('start.png')# 尝试一些测试w,h = 350,50a,b = 20, 80text = GenerateText((w,h), 32, 'black', 'magenta', "Magenta on black")im_o[a:a+h, b:b+w] = 文本w,h = 200,40a,b = 120, 280text = GenerateText((w,h), 18, 'cyan', 'blue', "Blue on cyan")im_o[a:a+h, b:b+w] = 文本cv2.imwrite('result.png', im_o)

关键字:OpenCV、Python、Numpy、PIL、Pillow、图像、图像处理、等宽、字体、字体、固定、固定宽度、快递、HERSHEY.

Currently, I am able to insert some texts of HERSHEY font into images with openCV API (putText). But it seems openCV are not supporting any monospace font.

I was wondering how I can insert some Monospace or fixed-pitch texts into the image.

解决方案

You could use PIL/Pillow for that aspect quite easily. OpenCV images are numpy arrays, so you can make a Pillow Image from an OpenCV image with:

PilImage = Image.fromarray(OpenCVimage)

Then you can draw with a mono spaced font using code in my answer here. You only need the 3 lines after the comment "Get a drawing context".

Then you can convert back to OpenCV image with:

OpenCVimage = np.array(PilImage)

That might look like this:

#!/usr/local/bin/python3

from PIL import Image, ImageFont, ImageDraw
import numpy as np
import cv2

# Open image with OpenCV
im_o = cv2.imread('start.png')

# Make into PIL Image
im_p = Image.fromarray(im_o)

# Get a drawing context
draw = ImageDraw.Draw(im_p)
monospace = ImageFont.truetype("/Library/Fonts/Andale Mono.ttf",32)
draw.text((40, 80),"Hopefully monospaced",(255,255,255),font=monospace)

# Convert back to OpenCV image and save
result_o = np.array(im_p)
cv2.imwrite('result.png', result_o)


Alternatively, you could have a function generate a lump of canvas itself, write your text on it, and then splice it into your OpenCV image wherever you want. Something along these lines - though I have no idea of what flexibility you would require so I have not parameterised everything:

#!/usr/local/bin/python3

from PIL import Image, ImageFont, ImageDraw, ImageColor
import numpy as np
import cv2


def GenerateText(size, fontsize, bg, fg, text):
   """Generate a piece of canvas and draw text on it"""
   canvas = Image.new('RGB', size, bg)

   # Get a drawing context
   draw = ImageDraw.Draw(canvas)
   monospace = ImageFont.truetype("/Library/Fonts/Andale Mono.ttf",fontsize)
   draw.text((10, 10), text, fg, font=monospace)

   # Change to BGR order for OpenCV's peculiarities
   return cv2.cvtColor(np.array(canvas), cv2.COLOR_RGB2BGR)


# Open image with OpenCV
im_o = cv2.imread('start.png')


# Try some tests
w,h = 350,50
a,b = 20, 80
text = GenerateText((w,h), 32, 'black', 'magenta', "Magenta on black")
im_o[a:a+h, b:b+w] = text


w,h = 200,40
a,b = 120, 280
text = GenerateText((w,h), 18, 'cyan', 'blue', "Blue on cyan")
im_o[a:a+h, b:b+w] = text

cv2.imwrite('result.png', im_o)

Keywords: OpenCV, Python, Numpy, PIL, Pillow, image, image processing, monospace, font, fonts, fixed, fixed width, courier, HERSHEY.

这篇关于如何使用 opencv 将等宽字体插入图像中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆