Skip to content

Instantly share code, notes, and snippets.

@adibenc
Last active October 26, 2019 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adibenc/a8c8b6eba67ef5e1651a452c9898eaef to your computer and use it in GitHub Desktop.
Save adibenc/a8c8b6eba67ef5e1651a452c9898eaef to your computer and use it in GitHub Desktop.
text 2 image, japan
# -*- coding: utf-8 -*-
# text 2 img
from PIL import Image, ImageDraw, ImageFont
bg={
'white':(255, 255, 255),
'red':(160, 0, 0),
'darkblue':(0, 0, 80),
'db2':(25, 50, 100),
'black':(0,0,0),
'black':'black',
'grey':(125,125,125)
}
kanjis=[]
def text2img(text,outputfile='output.png'):
ftsize=50
# text2write
# 見五口
text=text
# dynamic bg size by text size
X=int((ftsize*len(text))/2+ftsize)
Y=ftsize*2
X=500
Y=500
scale=1
bgsize=(X*scale,Y*scale) #x,y
img = Image.new('RGB', bgsize, color = bg['db2'])
# usethis if py2
fnt = ImageFont.truetype('/home/x7a616d/.fonts/07YasashisaAntique.ttf', ftsize)
# fnt = ImageFont.truetype('/home/x7a616d/.fonts/font_1_ant-maru.ttf', ftsize)
# fnt = ImageFont.truetype('/home/x7a616d/.fonts/CODE2000.TTF', ftsize)
# fnt = ImageFont.truetype('/home/x7a616d/.fonts/times.ttf', ftsize)
# fnt = "arial"
d = ImageDraw.Draw(img)
x, y = d.textsize(text,font=fnt)
d.text(((X-x)/2,(Y-y)/2), text,
font=fnt, fill=bg['white'])
img.save(outputfile)
kanjis=open('dic-list.txt','r').read().split("\n")
for i,k in enumerate(kanjis):
fname=str(i)+'.png'
text2img(k,fname)
print("to",fname)
# break
print("done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment