Last active
May 23, 2021 09:28
-
-
Save nikaera/c1049708ff548b06cab0ae377adc4ac7 to your computer and use it in GitHub Desktop.
Embedding Japanese vertical writing characters into an image using Pillow.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Put the image file sample.jpeg in the same directory as main.py | |
from PIL import Image, ImageDraw, ImageFont | |
font_name = "/System/Library/Fonts/ヒラギノ角ゴシック W0.ttc" | |
font_size = 48 | |
font = ImageFont.truetype(font_name, font_size) | |
im = Image.open('sample.jpeg') | |
w, h = im.size | |
d = ImageDraw.Draw(im) | |
text = "bifdLcFCKXtFJZmPZhzdefjhhYTtuJPAYsR" | |
# split_number needs to be smaller or larger depending on the font and its size. | |
split_number = 11 | |
# ref: https://qiita.com/yasunori/items/551a7c20ef9b81474e2a | |
splits = [text[i: i+split_number] for i in range(0, len(text), split_number)] | |
top_right_margin = 13 | |
right_edge = w - top_right_margin | |
d.rectangle((right_edge, top_right_margin, right_edge - font_size * len(splits), font_size * split_number + top_right_margin), fill=(255, 255, 255), outline=(255, 0, 0)) | |
for index, item in enumerate(splits): | |
d.text((right_edge - (font_size / 2) - font_size * index, top_right_margin), item, fill="black", anchor="mt", font=font, direction="ttb") | |
im.save("test.png") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://qiita.com/nikaera/items/164ba0e19bd80e4778aa