Kadaj13
Kadaj13

Reputation: 1551

Writing Japanese texts on images using PIL package

I have a dataset of Japanese words and I want to use the PIL package to draw them on images (images with black background and white written text).

Here is some part of my codes:

# -*- coding: utf-8 -*-

from PIL import Image, ImageDraw, ImageFont
import textwrap
import numpy as np
import sys
import os
import re

def draw_my_text(txt):
    para = textwrap.wrap(txt, width = 23) #change width maybe
    img = Image.new("L", (width, height), color=0)   # "L": (8-bit pixels, black and white)
    font = ImageFont.truetype("./data/arial.ttf", font_size)
    draw = ImageDraw.Draw(img)
    
    cur_h, pad = 200, 10
    for line in para:
        w, h = draw.textsize(line, font=font)
        draw.text(((width - w)/2, cur_h), line, fill='white', font=font)
        cur_h += h + pad
        
    image_name = "img_" + str(counter)
    img.save('./' + folder_name + "/"+ test_f_name + "/" + image_name + '.png')
    f1.write(txt + os.linesep)
    f2.write("word" + os.linesep)


with open('./' + folder_name + '/list_of_labels_test_type_20210622.txt', 'w') as f2:
    with open ('./' + folder_name + '/list_of_labels_test_20210622.txt', 'w') as f1:
        for sentence in all_sentences:
            text = sentence
            img = Image.new("L", (width, height), color=0)   # "L": (8-bit pixels, black and white)
            font = ImageFont.truetype("./data/arial.ttf", font_size)
            draw = ImageDraw.Draw(img)
            w, h = draw.textsize(text, font=font)
            #print(w)
            if w > 950:
                ls = len(text)
                list_of_returned_spaces = findSpacePosition(text)
                #text = text_to_splittedText(text, list_of_returned_spaces)
                #fixme
            for j in range(n_stim_repetition):
                counter = draw_sentence(text, width, height, counter, font, font_size)
                f1.write(sentence[:-1] + os.linesep)
                f2.write("sentence" + os.linesep)
                counter = draw_words_of_sentence(text, width, height, counter, font, font_size) 
                counter = draw_sentence(text, width, height, counter, font, font_size)
                f1.write(sentence[:-1] + os.linesep)
                f2.write("sentence" + os.linesep)
                counter = draw_rest_blank(width, height, counter)
                f1.write("........" + os.linesep)
                f2.write("rest" + os.linesep)

The functions draw_(something), all call the

draw_my_text(txt)

for drawing text to image. I have already used these codes for drawing English words and they work very well.

However, when I use Japanese, only some squares are getting drawedenter image description here

My text file looks like this: enter image description here

How should I change my code to fix this problem?

Upvotes: 1

Views: 585

Answers (1)

Kadaj13
Kadaj13

Reputation: 1551

Instead of the "arial" font, I downloaded the MSMINCHO.TTF font (which is mainly for Japanese), and now everything works.

Upvotes: 1

Related Questions