Skip to content

Instantly share code, notes, and snippets.

@zed
Created January 28, 2015 15:36
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zed/07a8175ba07f393a6004 to your computer and use it in GitHub Desktop.
Save zed/07a8175ba07f393a6004 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""Save all Unicode characters as individual images.
Based on @JackNova's answer http://stackoverflow.com/a/22612295
To save as jpeg, install libjpeg before installing Pillow e.g.:
$ sudo apt-get install libjpeg-dev -y
$ pip install -U pillow # assuming a virtualenv is active
To install DejaVu fonts on Ubuntu:
$ sudo apt-get install ttf-dejavu -y
"""
import logging
import multiprocessing
import os
import sys
import unicodedata
from contextlib import closing
from PIL import Image, ImageDraw, ImageFont, ImageFilter # $ pip install pillow
from tqdm import tqdm # $ pip install tqdm
try:
unichr = unichr
except NameError: # Python 3
unichr = chr
destination_directory = u'u'
width, height = 500, 100
background_color = 255, 255, 255 # white
font_color = 0, 0, 0 # black
font_size = 36
unicode_font = ImageFont.truetype("DejaVuSans.ttf", font_size)
logger = multiprocessing.get_logger()
def sanitize(filename, replacement_char=u'\ufffd'):
# \0 and / can't be in POSIX filename
return filename.replace(u'\x00', replacement_char)\
.replace(u'/', replacement_char)
def init(): # executed while starting a new child process
os.chdir(destination_directory)
def save_as_image(unicode_ordinal):
char = unichr(unicode_ordinal)
try:
name = unicodedata.name(char)
except ValueError as e: # no such name
logger.debug(u"%s (U+%x): %s", char, unicode_ordinal, e)
return None
filename = sanitize(u'U+{ord:08x} {name} {char}.jpg'.format(
ord=unicode_ordinal, name=name, char=char))
image = Image.new("RGB", (width, height), background_color)
ImageDraw.Draw(image).text((10, 10), char, font=unicode_font, fill=font_color)
image.save(filename)
return filename
def save_as_image_mp(unicode_ordinal):
try:
return unicode_ordinal, save_as_image(unicode_ordinal), None
except Exception as e:
return unicode_ordinal, None, str(e)
def main():
multiprocessing.log_to_stderr().setLevel(logging.WARNING)
try:
os.mkdir(destination_directory)
except EnvironmentError:
pass
with closing(multiprocessing.Pool(initializer=init)) as pool:
all_ordinals = range(sys.maxunicode+1)
results = pool.imap_unordered(save_as_image_mp, all_ordinals, chunksize=1000)
for i, filename, error in tqdm(results, total=len(all_ordinals)):
if error is not None:
logger.warning("failed to save {}: {}".format(i, error))
if __name__ == "__main__":
multiprocessing.freeze_support()
main()
@ybdesire
Copy link

ybdesire commented Jun 6, 2018

Thanks for the code.
The code works but most of the generated image just display a box instead of the Unicode character.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment