Hello. This is codingwalks.
Since OpenCV does not natively support outputting Unicode, additional work is required to output Unicode to an image. The most commonly used method for this is to use a TrueType font (TTF) file and draw Unicode to the image with the help of the Pillow (PIL) library.
1. Basic OpenCV text output function
In OpenCV, you can annotate an image with text using the cv2.putText() function. However, this function mainly outputs only ASCII-based text such as English and numbers, and does not support output of Unicode character such as Korean.
import cv2
import numpy as np
# Create a blank image
image = np.zeros((512, 512, 3), np.uint8)
# Output English text
cv2.putText(image, 'OpenCV Text Example', (50, 200), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
# Output Korean text
cv2.putText(image, 'OpenCV 텍스트 예제', (50, 250), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
# Image Output
cv2.imshow('Text', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
This code outputs English text correctly, but when you input Korean, it outputs broken characters. To solve this, we need a different approach.
2. Alternative for printing Korean fonts - Using the Pillow library
To print Korean in OpenCV, you need to use the image processing library Pillow (PIL). Pillow provides powerful functions that can draw various texts and fonts on images.
2.1. Installing Pillow
First, you need to install the Pillow library to print Korean text. You can easily install it using pip.
pip install pillow
2.2. Prepare Korean font files
Pillow uses font files to output text, so a .ttf font file that supports Korean is required. For example, you can download and use a font such as Nanum Gothic.
2.3 Outputting Korean text with Pillow in OpenCV
Now let's look at how to output Korean text to an image by combining Pillow and OpenCV.
import cv2
import numpy as np
from PIL import ImageFont, ImageDraw, Image
# Create a blank image
image = np.zeros((512, 512, 3), np.uint8)
# Convert OpenCV image to Pillow's Image object
image_pil = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# Set the Korean font path (specify the exact location of the font file)
fontpath = "fonts/NanumGothic.ttf"
font = ImageFont.truetype(fontpath, 40)
# Creating a Draw object in Pillow
draw = ImageDraw.Draw(image_pil)
# Drawing Korean text
text = "OpenCV 텍스트 예제"
draw.text((50, 200), text, font=font, fill=(255, 255, 255))
# Convert back to OpenCV image
image = cv2.cvtColor(np.array(image_pil), cv2.COLOR_RGB2BGR)
# Image Output
cv2.imshow('Hangul Text', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
2.4 Code Description
- Convert image to Pillow: In order to process OpenCV's image (numpy array) in Pillow, convert the OpenCV image to Pillow's Image object.
- Set font: Use the ImageFont.truetype() function to specify the font file and size. Here, the font file must be a .ttf file that supports Korean, and the path must be specified accurately.
- Draw text: Use Pillow's ImageDraw object to draw text on the image. Korean text can be drawn with the draw.text() function, which supports text in various languages.
- Convert image back: Convert the image with added text back to OpenCV format so that it can be processed in OpenCV.
3. Conclusion
If you run the above code, you can see that the phrase “OpenCV Text Example” is printed on the image in white text on a black background. The problem of printing Korean characters, which was difficult to solve with only OpenCV’s basic functions, can be easily solved by using the Pillow library.
OpenCV does not provide a function to directly print Korean characters by default, but it can be easily solved with additional libraries such as Pillow. This method allows you to add text to images in various languages, not just Korean. Try applying it when you need to use Korean characters in image processing and annotation work.
If you found this post useful, please like and subscribe below. ^^
★ All contents are referenced from the link below. ★