How can I insert Monospace fonts into an image wit

2019-07-10 03:42发布

问题:

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.

回答1:

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.