Created
June 26, 2017 07:30
-
-
Save phondanai/94c449da514f655025c541dfcc45b5de to your computer and use it in GitHub Desktop.
Create picture of 'Number, Decimal Digit' from unicode table
This file contains hidden or 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
import sys | |
import unicodedata | |
from collections import defaultdict | |
from PIL import ImageFont, ImageDraw, Image | |
# Steal from https://stackoverflow.com/questions/14245893/efficiently-list-all-characters-in-a-given-unicode-category | |
def number_decimal_digit(): | |
unicode_category = defaultdict(list) | |
# very expensive here | |
for c in map(chr, range(sys.maxunicode + 1)): | |
unicode_category[unicodedata.category(c)].append(c) | |
return unicode_category['Nd'] # Number Decimal Digit | |
def draw_font_image(word_to_draw, filename): | |
font_full_path = "/path/to/your/font.ttf" | |
full_path = "/tmp/draw_font/{}.png".format(filename) | |
W, H = 1000, 250 | |
img = Image.new("RGBA", (W, H), (255, 255, 255)) | |
draw = ImageDraw.Draw(img) | |
font = ImageFont.truetype(font_full_path, 48) | |
#w, h = draw.textsize(word_to_draw) | |
draw.text((0, H/2), word_to_draw, (0, 0, 0), font=font) | |
draw = ImageDraw.Draw(img) | |
print("Saving....") | |
print(full_path) | |
img.save(full_path) | |
print("Done.") | |
number_decimal = number_decimal_digit() | |
# Loop 0-9 number for each language | |
for i in range(0, len(number_decimal), 10): | |
word_to_draw = " ".join(number_decimal[i:i+10]) | |
if i == 0: | |
file_name = ''.join(unicodedata.name(number_decimal[i]).split(' ')[0]) | |
else: | |
file_name = ''.join(unicodedata.name(number_decimal[i]).split(' ')[:-2]) | |
draw_font_image(word_to_draw, file_name) | |
#print(word_to_draw, file_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment