3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pillowで画像に日本語を書き込む

Posted at

やりたいこと

テキストを画像に書き込み,はみ出そうなら次の行に改行する

実行環境

macOS Mojava 10.14
python 3.5.5
Pillow 5.1.0

サンプルコード

text.txt
あいうえお安以宇衣於アイウエオ阿伊宇江於かきくけこ…
put_text.py
# -*- coding:utf8 -*-
import numpy as np
from PIL import Image, ImageDraw, ImageFont

def draw_txt(background, word, f_path, t_size , t_color, t_cood,t_pad):
    # フォント設定
    font = ImageFont.truetype(f_path, t_size)
    # テキスト入力用クラス宣言
    draw = ImageDraw.Draw(background)
    # 背景に書き込み
    cnt = 0
    for y in range(t_cood[0], background.size[1]-t_cood[0], t_size+t_pad[0]):
        for x in range(t_cood[1], background_size[0]-t_cood[1], t_size+t_pad[1]):
            if len(word) > cnt:
                draw.text((x,y), word[cnt], font=font, fill=t_color)
            else:
                break
            cnt += 1
    return background


if __name__ == "__main__":
    # 背景作成
    background_size = (320,240)
    background_color = (0,0,0,255)
    background = Image.new('RGBA', background_size, background_color)
    #テキスト指定
    word = str(np.genfromtxt('word.txt', delimiter=',', dtype=np.str))
    font_path = '/System/Library/Fonts/ヒラギノ角ゴシック W4.ttc'
    text_size = 20
    text_color = (180,180,180,255)
    #y,x
    text_coordinate = (20,20)
    text_padding = (10,0)
    draw_img = draw_txt(background, word, font_path, text_size, text_color, text_coordinate, text_padding)
    #画像指定
    draw_img.show()
    #画像保存
    save_path = './text.png'
    draw_img.save(save_path)

出力結果

text.png

問題点

全角しか対応していない(やりたかったのが全角のみだったから)

参考サイト

https://pillow.readthedocs.io/en/3.1.x/reference/Image.html
https://pillow.readthedocs.io/en/5.1.x/reference/ImageFont.html
https://pillow.readthedocs.io/en/5.1.x/reference/ImageDraw.html

3
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?