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

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

问题描述

当前,我能够使用openCV API(putText)将HERSHEY字体的某些文本插入图像中.但似乎openCV不支持任何等宽字体.

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.

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

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

推荐答案

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

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)

然后您可以在我的答案的此处中使用代码以单色字体绘制.您只需在注释获取工程图上下文" 之后的3行.

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".

然后您可以使用以下方法将其转换回OpenCV图像:

Then you can convert back to OpenCV image with:

OpenCVimage = np.array(PilImage)

可能看起来像这样:

#!/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)

或者,您可以让一个函数本身生成一个画布块,在画布上写您的文本,然后将其拼接到您想要的任意位置的OpenCV图像中.遵循这些原则-尽管我不知道您需要什么灵活性,所以我没有参数化所有内容:

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)

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

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

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

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