How can I insert Monospace fonts into an image with opencv?
Asked Answered
D

1

3

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.

Disputatious answered 9/12, 2018 at 20:12 Comment(0)
F
6

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)

Note that you are not limited to monospaced fonts, you can use any Truetype font you like.

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)

enter image description here


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)

enter image description here

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

Fatally answered 9/12, 2018 at 22:9 Comment(5)
Since the first example does not convert OpenCV's BGR to RGB, passing, e.g., 'blue' to draw.text() will draw red text. Also, it seems the font locations are quite OS-specific; On Ubuntu 16.04, /usr/share/fonts/truetype/freefont/FreeMono.ttf worked for me.Bruxelles
@UlrichStern Yes, that's correct. I was showing a concept more than a completely tested, generalised, production-ready project. It often turns out that OPs don't give a full and complete specification of their needs and some clue as to how to get started is enough.Fatally
@UlrichStern I guess you could pass the colour as a BGR triplet in a tuple and reverse it...Fatally
I appreciated your answer and did upvote. Due to the OS-dependency of monospace, however, I ended up adding adding "column support" to my OvenCV-based putText() utility function via measuring text widths instead.Bruxelles
The fact that I used a macOS font is not a "system dependency" - you can grab and use any TrueType font you like from anywhere.Fatally

© 2022 - 2024 — McMap. All rights reserved.