October 11, 2013
Python Character Image Generator
I wanted to generate unicode character images with certain size, font and color. I ended up writing the code below. I thought of sharing the code as it might be useful for some people.
Image Font Generator Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# -*- coding: utf-8 -*- #-------------------------------- Imports ------------------------------# # Import python imaging libs from PIL import Image from PIL import ImageDraw from PIL import ImageFont # Import operating system lib import os # Import random generator from random import randint #-------------------------------- Cleanup ------------------------------# def Cleanup(): # Delete ds_store file if os.path.isfile(font_dir + '.DS_Store'): os.unlink(font_dir + '.DS_Store') # Delete all files from output directory for file in os.listdir(out_dir): file_path = os.path.join(out_dir, file) if os.path.isfile(file_path): os.unlink(file_path) return #--------------------------- Generate Characters -----------------------# def GenerateCharacters(): # Counter k = 1 # Process the font files for dirname, dirnames, filenames in os.walk(font_dir): # For each font do for filename in filenames: # Get font full file path font_resource_file = os.path.join(dirname, filename) # For each character do for char in characters: # For each font size do for font_size in font_sizes: if font_size > 0: # For each background color do for background_color in background_colors: # Convert the character into unicode character = unicode(char, 'utf-8') # Create character image : # Grayscale, image size, background color char_image = Image.new('L', (image_size, \ image_size), background_color) # Draw character image draw = ImageDraw.Draw(char_image) # Specify font : Resource file, font size font = \ ImageFont.truetype(font_resource_file, font_size) # Get character width and height (font_width, font_height) = font.getsize(character) # Calculate x position x = (image_size - font_width)/2 # Calculate y position y = (image_size - font_height)/2 # Draw text : Position, String, # Options = Fill color, Font draw.text((x, y), character, (245- \ background_color) + randint(0, 10) , font=font) # Final file name file_name = out_dir + str(k) + '_' + \ filename + '_fs_' + \ str(font_size) + '_bc_' + \ str(background_color) + '.' + \ character + '.png' # Save image char_image.save(file_name) # Print character file name print file_name; # Increment counter k = k + 1 return #------------------------------- Input and Output ------------------------# # Directory containing fonts font_dir = '/path/to/fonts/dir/' # Output out_dir = '/path/to/output/dir' #---------------------------------- Characters ---------------------------# # Numbers numbers = ['0', '1', '2'] # Small letters small_letters = ['a', 'b', 'c'] # Capital letters capital_letters = ["A", 'B', 'C'] # Select characters characters = numbers + small_letters + capital_letters #---------------------------------- Colors -------------------------------# # Background color white_colors = (215, 225, 235, 245) black_colors = (0, 10, 20, 30) gray_colors = (135, 145, 155) background_colors = white_colors + black_colors + gray_colors #----------------------------------- Sizes -------------------------------# # Character sizes small_sizes = (8, 12, 16) medium_sizes = (20, 24, 28) large_sizes = (32, 36, 40) font_sizes = small_sizes + medium_sizes + large_sizes # Image size image_size = 64 #----------------------------------- Main --------------------------------# # Do cleanup Cleanup() # Generate characters GenerateCharacters() |
More from my site
One Comment
I need to create handwritten font with the help of strokes . is this helpful? or provide me some solution for the same.