Reputation: 462
I would like to know if it is possible to write an emoji on an image with OpenCV.
This is what I have so far.
import cv2
import emoji
img = cv2.imread('image.png')
tick=str(emoji.emojize(':heavy_check_mark:'))
cv2.putText(img, tick,(5,5), cv2.FONT_HERSHEY_TRIPLEX , 1, (255, 255, 255),3)
cv2.imshow('result',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
The above code does not work and I don't know why. Is it possible to load the tick emoji as an image (say img2) and then 'paste' it on some coordinate?
I am using python 3.7.6 on a windows.
Upvotes: 2
Views: 3812
Reputation: 207853
You can use PIL/Pillow and a suitable font like this:
#!/usr/local/bin/python3
from PIL import Image, ImageFont, ImageDraw
import numpy as np
import cv2
import emoji
# 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)
font = ImageFont.truetype("Arial Unicode.ttf",32)
tick=str(emoji.emojize(':heavy_check_mark:'))
draw.text((40, 80),tick,(255,255,255),font=font)
# Convert back to OpenCV image and save
result_o = np.array(im_p)
cv2.imwrite('result.png', result_o)
Note that Red and Blue channels are interchanged in OpenCV, so either reverse the channel ordering or use blacks, whites and greens. Reverse channel like this:
BGRimage = RGBimage[...,::-1]
Upvotes: 1
Reputation: 23556
Please, check this answer Load TrueType Font to OpenCV about loading TTF fonts in OpenCV:
Upvotes: 0