10
2

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.

PIL で日本語の文字を書く

Last updated at Posted at 2018-03-20

ツルータイプのフォントを使って PIL で画像を作成しました。

out01.png

draw_text.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#	draw_text.py
#
#					Mar/20/2018
#
# ------------------------------------------------------------------
import sys
import numpy

import PIL.Image
import PIL.ImageDraw
import PIL.ImageFont

# ------------------------------------------------------------------
def draw_text_at_center(img, text):
	draw = PIL.ImageDraw.Draw(img)
	font_ttf = "/usr/share/fonts/OTF/TakaoPMincho.ttf"
	draw.font = PIL.ImageFont.truetype(font_ttf, 80)

	img_size = numpy.array(img.size)
	txt_size = numpy.array(draw.font.getsize(text))
	pos = (img_size - txt_size) / 2

	draw.text(pos, text, (0, 0, 255))

# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
img = PIL.Image.new("RGBA", (400, 300))
text = "こんにちは"
draw_text_at_center(img, text)
img.show()
filename = "out01.png"
img.save(filename)
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------
10
2
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
10
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?