I referred to this. Watlab | How to put Japanese characters in images with Python https://watlab-blog.com/2019/08/25/image-text/ How to put your favorite font in Google Colaboratory and use it with matplotlib https://qiita.com/nkay/items/b2d50349a3f5d38df45b
font.py
#Install fonts locally in Colab
from google.colab import drive
drive.mount("/content/gdrive")
!cp -a "gdrive/My Drive/font/" "/usr/share/fonts/"
from PIL import Image, ImageFont, ImageDraw
import cv2
import numpy as np
from google.colab.patches import cv2_imshow
#Function to put text in image
def img_add_msg(img, message):
font_path = '/usr/share/fonts/meiryo.ttc' #Path to Colab fonts
font_size = 100
font = ImageFont.truetype(font_path, font_size) #Define fonts in PIL
img = Image.fromarray(img) # cv2(NumPy)Convert type image to PIL type
draw = ImageDraw.Draw(img) #Draw function for drawing
#Draw text (position, text, font, text color (BGR)+α) is specified)
draw.text((50, 50), message, font=font, fill=(255, 255, 255, 0))
img = np.array(img) #PIL type image cv2(NumPy)Convert to type
return img #Returns an image with text
img = cv2.imread('/content/gdrive/My Drive/hoge/huga.jpg', 1) #Image loading
message = 'Hello World' #Japanese to put in the image
img = img_add_msg(img, message)
#Image display
cv2_imshow(img)
Yes.
Recommended Posts