from PIL import Image, ImageDraw, ImageFont import numpy as np import json def text_phantom(text, size): # Availability is platform dependent font = 'arial' # Create font font = ImageFont.truetype("../resources/GothamMedium.ttf", 25) text_width, text_height = font.getsize(text) # create a blank canvas with extra space between lines canvas = Image.new('RGB', [size, size], (255, 255, 255)) # draw the text onto the canvas draw = ImageDraw.Draw(canvas) offset = (2,5) white = "#000000" draw.text(offset,text,font=font,fill=white) # Convert the canvas into an array with values in [0, 1] return (255 - np.asarray(canvas)) / 255.0 alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ???" data_dict = {} for i in range(6): for j in range(6): index = i*6+j if index < len(alphabet): data_dict[alphabet[index]] = text_phantom(alphabet[index], 25) def find_random_points_pos(image, N=10000): indices = np.random.randint(0, 25, (N, 2)) letter_points = indices[np.sum(image[indices[:,0], indices[:,1]], axis=1)>1] letter_points[:,0] = -letter_points[:,0]+25 letter_points[:,:] = letter_points[:,::-1] return letter_points point_clouds = {} italic_point_clouds = {} transformation_matrix = np.matrix([[1,0.3], [0, 1]]) for letter in alphabet: point_clouds[letter] = find_random_points_pos(data_dict[letter]) italic_point_clouds[letter] = np.asarray(np.matmul(transformation_matrix, point_clouds[letter].T).T) for key in italic_point_clouds.keys(): italic_point_clouds[key] = italic_point_clouds[key].tolist() for key in point_clouds.keys(): point_clouds[key] = point_clouds[key].tolist() json.dump(point_clouds, open("../resources/straight_letters.json", "w")) json.dump(italic_point_clouds, open("../resources/italic_letters.json", "w"))