I'm building a online font previewer, with following architecture. I wrapped preview creation function in a standalone .py file and making system calls to it in a Django view in order to run them in parallel and maximum performance on multi-core CPU system.
preview.py
....
def make_preview(text, fontfile, imagefile, fontsize=30):
try:
font = ImageFont.truetype(fontfile, fontsize)
text_width, text_height = font.getsize(text)
img = Image.new('RGBA', (text_width, text_height))
draw = ImageDraw.Draw(img)
draw.text((0, 0), text, font=font, fill=(0, 0, 0))
return True
except:
return False
if __name__=='__main__':
make_preview(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
views.py
...
def ajax_preview(resquest):
ttf_file_list = [construct a list of .ttf files]
image_list = []
for ttf_file in ttf_file_list:
image_file = ttf_file.replace('.ttf', '.png')
image_list.append(image_file)
subprocess.Popen(['python', 'preview.py', text, ttf_file, image_file, 30)
return HttpResponse(image_list)
Is it possible to make above code even faster? Maybe there is a better architecture to do this job or any optimization I can do for the code?