[PYTHON] Put Japanese fonts in images with Colaboratory

  1. Put fonts locally in Colab
  2. Load images with OpenCV
  3. Font drawing with PIL
  4. Image display with OpenCV

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. img

Recommended Posts

Put Japanese fonts in images with Colaboratory
Number recognition in images with Python
Put postfix 2.11 in source with ansible
Working with DICOM images in Python
Put TensorFlow in P2 instance with pip3
Read text in images with python OCR
Draw a graph with Japanese labels in Jupyter
Convert PDFs to images in bulk with Python
Handle zip files with Japanese filenames in Python 3
Create an image with characters in python (Japanese)
Japanese with matplotlib
Put AWS data in Google Spreadsheet with boto + gspread
How to load files in Google Drive with Google Colaboratory
Make a function to describe Japanese fonts with OpenCV
Japanese output when dealing with python in visual studio
How to use Spacy Japanese model in Google Colaboratory
Edit fonts in Python
Put scipy in ec2
Put jedi in emacs 24
Japanese input with pyautogui
View images in Matplotlib
Speaking Japanese with OpenJtalk
Put pip in Blender
Japanese output in Python
English PDF in Japanese
Center images with python-pptx
Use "% tensorflow_version 2.x" when using TPU with Tensorflow 2.1.0 in Colaboratory
Create SVG graph with matplotlib on heroku (displayed in Japanese)
Make the function of drawing Japanese fonts in OpenCV general
Avoid UnicodeEncodeError when throwing queries with Japanese in Python's MySQLdb
Issue reverse geocoding in Japanese with Python Google Maps API