def draw_watermark(self, img): watermark_draw = Drawing() watermark_draw.stroke_color = Color('black') watermark_draw.font = os.path.join(os.path.dirname(__file__), "Colombia-Regular.ttf") watermark_draw.font_size = 3 * POINT_PER_MM watermark_draw.text_antialias = True metrics = watermark_draw.get_font_metrics(img, WATERMARK) watermark_draw.text(max(0, math.floor((POINT_PER_MM * self.paper['width']) - metrics.text_width)), max(0, math.floor((POINT_PER_MM * self.paper['height']) - metrics.text_height * 2)), WATERMARK) watermark_draw.draw(img)
def render_text_line(text, font, size, weight, color=Color('none'), background=None): match = re.match("(\w+)(-\w+)?", weight) italic = None if match: weight = match.group(1) italic = match.group(2) if not italic: italic = "" # # Special case: blank line # if text == "": graphic = new_blank_png(width=5, height=5) return (5, 5, graphic) sequence = lex_line(text, weight) images = [] dummy_image = Image(width=100, height=100, background=None) for (level, text) in sequence: draw = Drawing() draw.font = FONT_INFO[font][level+italic] draw.font_size = size draw.fill_color = color metrics = draw.get_font_metrics(dummy_image, text, False) image = new_blank_png(width=int(metrics.text_width), height=int(metrics.y2-metrics.y1)) draw.text(0, int(metrics.y2), text) draw(image) images.append((metrics.y2, metrics.y1, image)) if images == []: return None max_ascender = max([y2 for (y2,_,_) in images]) max_descender = -1 * min([y1 for (_,y1,_) in images]) final_image_width = sum([i.width for (_,_,i) in images]) final_image_height = int(max_ascender + max_descender) final_image = new_blank_png(width=final_image_width, height=final_image_height, color=background) top_offset = 0 for (y2,y1,i) in images: final_image.composite(i, top_offset, int(max_ascender-y2)) top_offset = top_offset + i.width return (max_ascender, max_descender, final_image)
def generate(count): # ---------------get three colors------------------------------- colorString = random.choice(result) color = [] for i in colorString: color += [int(i)] # for i in range(len(color)): # color[i] = math.floor(color[i]/255*65535) color1 = color[0:3] color2 = color[3:6] color3 = color[6:9] # -------------------------------------------------------------- # ----------get the base layer texture-------------------------- Scenes = pathwalk('./SceneData') randomScene = random.choice(Scenes) randomScene = randomScene[0] + '/' + randomScene[1] print(randomScene) randomSceneImage = Image(filename=randomScene) widthRange = randomSceneImage.size[0] - 100 heightRange = randomSceneImage.size[1] - 32 randomSceneImage.crop(left=random.randint(0, widthRange), top=random.randint(0, heightRange), width=100, height=32) # randomSceneImage.save(filename='.\\photoWand\\'+str(j+1) + '_texture.jpg') # -------------------------------------------------------------- # ----------create the base layer, base texture +base color----- baseImage = Image( width=100, height=32, background=Color('rgb(' + str(color1[0]) + ',' + str(color1[1]) + ',' + str(color1[2]) + ')')) # print('base_color = ' + 'rgb('+str(color1[0])+','+str(color1[1])+','+str(color1[2])+')') baseImage.composite_channel(channel='undefined', image=randomSceneImage, operator='blend', left=0, top=0) baseImage.gaussian_blur(4, 10) baseImage.resolution = (96, 96) # -------------------------------------------------------------- # -----generate font-------------------------------------------- word = randomWords() fonts = pathwalk('./fonts/font_en/') randomFont = random.choice(fonts) randomFont = randomFont[0] + randomFont[1] initialPointsize = 45 draw = Drawing() draw.font = randomFont tmp = int(math.floor(abs(random.gauss(0, 1)) * 6)) if random.randint(1, 2) == 1: rotateX = random.randint(0, tmp) else: rotateX = random.randint(360 - tmp, 360) draw.rotate(rotateX) # -------------------------------------------------------------- # --------get suitable FontPointSize---------------------------- draw.font_size = initialPointsize metric = draw.get_font_metrics(image=baseImage, text=word) while metric.text_width > 100 or metric.text_height > 36: initialPointsize -= 5 draw.font_size = initialPointsize metric = draw.get_font_metrics(image=baseImage, text=word) # -------------------------------------------------------------- # ----------italic---------------------------------------------- if random.random() > 0.5: draw.font_style = 'italic' # -------------------------------------------------------------- # ----------underline------------------------------------------- if random.random() > 0.5: draw.text_decoration = 'underline' # -------------------------------------------------------------- # ----------gravity--------------------------------------------- draw.gravity = 'center' # -------------------------------------------------------------- # --------------shadow/border----------------------------------- if random.random() < 0.5: # shadow addx = math.ceil(random.gauss(0, 2)) addy = math.ceil(random.gauss(0, 2)) draw.fill_color = Color('black') draw.text(x=abs(int(addx)), y=abs(int(addy)), body=word) else: # border draw.stroke_color = Color('rgb(' + str(color3[0]) + ',' + str(color3[1]) + ',' + str(color3[2]) + ')') draw.stroke_width = math.ceil(initialPointsize / 10) - 1 # -------------------------------------------------------------- # ----------print word------------------------------------------ draw.fill_color = Color('rgb(' + str(color2[0]) + ',' + str(color2[1]) + ',' + str(color2[2]) + ')') draw.text(x=0, y=0, body=word) draw.draw(baseImage) # -------------------------------------------------------------- # ------------gray---------------------------------------------- baseImage.colorspace = 'gray' # -------------------------------------------------------------- print(word) baseImage.save(filename='./photo_en/' + str(count + 1) + '_' + word + '.jpg')
def main(): args = get_args() draw = Drawing() draw.font = args.font_file draw.font_size = args.font_size font_name = args.font_name out_dir = args.out_dir img_ref = Image(width=1000, height=1000) if args.verbose: print "Writing " + out_dir + "/" + font_name + ".c" f = open(out_dir + "/" + font_name + ".c", 'wb+') write_comment(f) f.write("#include \"font.h\"\n\n") font_height = 0 range_first = 0x20 range_last = 0x7d font_width = [] max_width = 0 for x in range(range_first, range_last + 1): letter = chr(x) metrics = draw.get_font_metrics(img_ref, letter) text_height = int(round(metrics.text_height + 2)) if font_height == 0: font_height = text_height assert (font_height == text_height), "font height changed!" if max_width == 0: max_width = metrics.maximum_horizontal_advance + 2 assert (max_width == metrics.maximum_horizontal_advance + 2), \ "font advance width changed!" text_width = int(round(metrics.text_width + 2)) font_width.append(text_width) img = Image(width=text_width, height=text_height) d = draw.clone() d.text(0, int(metrics.ascender), letter) d(img) img.depth = 1; f.write("static const unsigned char ") f.write("letter_" + str(hex(x)[2:]) + "[] = {\n") c_hex_print(f, img.make_blob(format='A')) f.write("};\n\n") img.close() f.write("static const struct font_letter letters[] = {\n") for x in range(range_first, range_last + 1): letter_var_name = "letter_" + str(hex(x)[2:]) f.write("\t{ " + letter_var_name + ", ") f.write("sizeof(" + letter_var_name + "), ") f.write(str(font_width[x - range_first]) + "},\n") f.write("};\n\n") f.write("const struct font font_" + font_name + " = {\n") f.write("\t.first = " + str(hex(range_first)) + ",\n") f.write("\t.last = " + str(hex(range_last)) + ",\n") f.write("\t.letters = letters,\n") f.write("\t.height = " + str(font_height) + ",\n") f.write("\t.max_width = " + str(max_width) + ",\n") f.write("};\n") f.close() if args.verbose: print "Writing " + out_dir + "/" + font_name + ".h" f = open(out_dir + "/" + font_name + ".h", 'wb+') write_comment(f) f.write("#ifndef __" + font_name.upper() + "_H\n"); f.write("#define __" + font_name.upper() + "_H\n"); f.write("#include \"font.h\"\n") f.write("extern const struct font font_" + font_name + ";\n") f.write("#endif /*__" + font_name.upper() + "_H*/\n"); f.close()
initialPointsize = 45 draw = Drawing() draw.font = randomFont tmp = int(math.floor(abs(random.gauss(0, 1))*6)) if random.randint(1, 2) == 1: rotateX = random.randint(0, tmp) else: rotateX = random.randint(360-tmp, 360) draw.rotate(rotateX) # -------------------------------------------------------------- # --------get suitable FontPointSize---------------------------- draw.font_size = initialPointsize metric = draw.get_font_metrics(image=baseImage, text=word) while metric.text_width > 100 or metric.text_height > 36: initialPointsize -= 5 draw.font_size = initialPointsize metric = draw.get_font_metrics(image=baseImage, text=word) # -------------------------------------------------------------- # ----------italic---------------------------------------------- if random.random() > 0.5: draw.font_style = 'italic' # -------------------------------------------------------------- # ----------underline------------------------------------------- if random.random() > 0.5: draw.text_decoration = 'underline'
def main(): args = get_args() draw = Drawing() draw.font = args.font_file draw.font_size = args.font_size font_name = args.font_name out_dir = args.out_dir img_ref = Image(width=1000, height=1000) if args.verbose: print "Writing " + out_dir + "/" + font_name + ".c" f = open(out_dir + "/" + font_name + ".c", 'wb+') write_comment(f) f.write("#include \"font.h\"\n\n") font_height = 0 range_first = 0x20 range_last = 0x7d font_width = [] max_width = 0 for x in range(range_first, range_last + 1): letter = chr(x) metrics = draw.get_font_metrics(img_ref, letter) text_height = int(round(metrics.text_height + 2)) if font_height == 0: font_height = text_height assert (font_height == text_height), "font height changed!" if max_width == 0: max_width = metrics.maximum_horizontal_advance + 2 assert (max_width == metrics.maximum_horizontal_advance + 2), \ "font advance width changed!" text_width = int(round(metrics.text_width + 2)) font_width.append(text_width) img = Image(width=text_width, height=text_height) d = draw.clone() d.text(0, int(metrics.ascender), letter) d(img) img.depth = 1 f.write("static const unsigned char ") f.write("letter_" + str(hex(x)[2:]) + "[] = {\n") c_hex_print(f, img.make_blob(format='A')) f.write("};\n\n") img.close() f.write("static const struct font_letter letters[] = {\n") for x in range(range_first, range_last + 1): letter_var_name = "letter_" + str(hex(x)[2:]) f.write("\t{ " + letter_var_name + ", ") f.write("sizeof(" + letter_var_name + "), ") f.write(str(font_width[x - range_first]) + "},\n") f.write("};\n\n") f.write("const struct font font_" + font_name + " = {\n") f.write("\t.first = " + str(hex(range_first)) + ",\n") f.write("\t.last = " + str(hex(range_last)) + ",\n") f.write("\t.letters = letters,\n") f.write("\t.height = " + str(font_height) + ",\n") f.write("\t.max_width = " + str(max_width) + ",\n") f.write("};\n") f.close() if args.verbose: print "Writing " + out_dir + "/" + font_name + ".h" f = open(out_dir + "/" + font_name + ".h", 'wb+') write_comment(f) f.write("#ifndef __" + font_name.upper() + "_H\n") f.write("#define __" + font_name.upper() + "_H\n") f.write("#include \"font.h\"\n") f.write("extern const struct font font_" + font_name + ";\n") f.write("#endif /*__" + font_name.upper() + "_H*/\n") f.close()
from wand.drawing import Drawing from wand.display import display weight_sequence = ['ultralight', 'light', 'regular', 'bold', 'black'] image_width = 400 image_height = 60 dummy_image = Image(width=image_width, height=image_height, background=Color('white')) draw = Drawing() draw.font = '/Users/zastre/Dev/fimsi/sandbox/fonts/LatoOFL/TTF/Lato-Reg.ttf' draw.font_size = 36 draw.fill_color = Color('black') metrics_01 = draw.get_font_metrics(dummy_image, "21", False) draw.font = '/Users/zastre/Dev/fimsi/sandbox/fonts/LatoOFL/TTF/Lato-Bla.ttf' draw.font_size = 36 draw.fill_color = Color('black') metrics_02 = draw.get_font_metrics(dummy_image, "CV", False) image_01 = Image(width=int(metrics_01.text_width), height=int(metrics_01.text_height), background=None) draw_01 = Drawing() draw_01.font = '/Users/zastre/Dev/fimsi/sandbox/fonts/LatoOFL/TTF/Lato-Reg.ttf' draw_01.font_size = 36 draw_01.fill_color = Color('black') draw_01.text(0, int(metrics_01.text_height), "21") draw_01(image_01)
initialPointsize = 45 draw = Drawing() draw.font = randomFont tmp = int(math.floor(abs(random.gauss(0, 1)) * 6)) if random.randint(1, 2) == 1: rotateX = random.randint(0, tmp) else: rotateX = random.randint(360 - tmp, 360) draw.rotate(rotateX) # -------------------------------------------------------------- # --------get suitable FontPointSize---------------------------- draw.font_size = initialPointsize metric = draw.get_font_metrics(image=baseImage, text=word) while metric.text_width > 100 or metric.text_height > 36: initialPointsize -= 5 draw.font_size = initialPointsize metric = draw.get_font_metrics(image=baseImage, text=word) # -------------------------------------------------------------- # ----------italic---------------------------------------------- if random.random() > 0.5: draw.font_style = 'italic' # -------------------------------------------------------------- # ----------underline------------------------------------------- if random.random() > 0.5: draw.text_decoration = 'underline'
import argparse import sys import yaml import re from wand.image import Image from wand.color import Color from wand.drawing import Drawing from wand.display import display image_width = 400 image_height = 60 image = Image(width=image_width, height=image_height, background=Color('white')) draw = Drawing() draw.font = '/Users/zastre/Dev/fimsi/sandbox/fonts/LatoOTF/TTF/Lato-Reg.ttf' draw.font_size = 36 draw.fill_color = Color('black') metrics = draw.get_font_metrics(image, "losing photos", False) pos_col = int((image_width - metrics.text_width)/2) pos_row = int(metrics.ascender + (image_height - metrics.text_height)/2) print "DEBUG ", pos_col, pos_row draw.text(pos_col, pos_row, "losing photos") draw(image) image.save(filename="_00.png")
import argparse import sys import yaml import re from wand.image import Image from wand.color import Color from wand.drawing import Drawing from wand.display import display image_width = 400 image_height = 60 image = Image(width=image_width, height=image_height, background=Color('white')) draw = Drawing() draw.font = '/Users/zastre/Dev/fimsi/sandbox/fonts/LatoOTF/TTF/Lato-Reg.ttf' draw.font_size = 36 draw.fill_color = Color('black') metrics = draw.get_font_metrics(image, "losing\nphotos", True) pos_col = int((image_width - metrics.text_width)/2) pos_row = int(metrics.ascender + (image_height - metrics.text_height)/2) print "DEBUG ", pos_col, pos_row draw.text(pos_col, pos_row, "losing\nphotos") draw(image) image.save(filename="_00.png")
from wand.display import display #full_text = ["The first line", "and a second line", "... finally a third line."] full_text = ["The first line"] FONT_SIZE = 48 dummy_image = Image(width=100, height=100, background=Color('black')) draw = Drawing() draw.font = '/Users/zastre/Dev/fimsi/sandbox/fonts/LatoOFL/TTF/Lato-Reg.ttf' draw.font_size = FONT_SIZE draw.fill_color = Color('black') rendered_segments = [] for text in full_text: metrics = draw.get_font_metrics(dummy_image, text, False) yMax = int(metrics.y2) yMin = -1 * int(metrics.y1) segment_width = int(metrics.text_width) draw = Drawing() draw.font = '/Users/zastre/Dev/fimsi/sandbox/fonts/LatoOFL/TTF/Lato-Reg.ttf' draw.font_size = FONT_SIZE draw.fill_color = Color('black') draw.text(0, yMax, text) image = Image(width=segment_width, height=yMax+yMin, background=None) draw(image) rendered_segments.append((image, yMax, yMin)) line_spacing = FONT_SIZE * 1.1 (_, yMax, _) = rendered_segments[0] (_, _, yMin) = rendered_segments[-1]
# Create a blank image image_width = 300 image_height = 400 img = Image(width=image_width, height=image_height, background=Color('rgb(44,128,64)')) # Need a drawing object for rendering. # Here there are some canvas (?) settings being established d = Drawing() d.fill_color = Color('rgb(255,255,255)') d.font = 'Arvo-Regular.ttf' d.font_size = 48 # Want some info on how big the text will be # when drawn, then use it for calculations # fm = d.get_font_metrics(img, 'Hello!') height = fm.text_height width = fm.text_width pos_x = int((image_width - width) / 2) pos_y = int((image_height) / 2) # Specify the coordinate of the lower-left # position of the text (where 0,0 is the # upper-left hand corner o the canvas). # d.text(pos_x, pos_y, 'Hello!') d(img) # Save the result # img.save(filename='300x400-red.png')
def generate(count): # ---------------get three colors------------------------------- colorString = random.choice(result) color = [] for i in colorString: color += [int(i)] # for i in range(len(color)): # color[i] = math.floor(color[i]/255*65535) color1 = color[0:3] color2 = color[3:6] color3 = color[6:9] # -------------------------------------------------------------- # ----------get the base layer texture-------------------------- Scenes = pathwalk('./SceneData') randomScene = random.choice(Scenes) randomScene = randomScene[0] + '/' + randomScene[1] print(randomScene) randomSceneImage = Image(filename=randomScene) widthRange = randomSceneImage.size[0] - 100 heightRange = randomSceneImage.size[1] - 32 randomSceneImage.crop(left=random.randint(0, widthRange), top=random.randint(0, heightRange), width=100, height=32) # randomSceneImage.save(filename='.\\photoWand\\'+str(j+1) + '_texture.jpg') # -------------------------------------------------------------- # ----------create the base layer, base texture +base color----- baseImage = Image(width=100, height=32, background=Color('rgb('+str(color1[0])+','+str(color1[1])+','+str(color1[2])+')')) # print('base_color = ' + 'rgb('+str(color1[0])+','+str(color1[1])+','+str(color1[2])+')') baseImage.composite_channel(channel='undefined', image=randomSceneImage, operator='blend', left=0, top=0) baseImage.gaussian_blur(4, 10) baseImage.resolution = (96, 96) # -------------------------------------------------------------- # -----generate font-------------------------------------------- word = randomWords() fonts = pathwalk('./fonts/font_en/') randomFont = random.choice(fonts) randomFont = randomFont[0] + randomFont[1] initialPointsize = 45 draw = Drawing() draw.font = randomFont tmp = int(math.floor(abs(random.gauss(0, 1))*6)) if random.randint(1, 2) == 1: rotateX = random.randint(0, tmp) else: rotateX = random.randint(360-tmp, 360) draw.rotate(rotateX) # -------------------------------------------------------------- # --------get suitable FontPointSize---------------------------- draw.font_size = initialPointsize metric = draw.get_font_metrics(image=baseImage, text=word) while metric.text_width > 100 or metric.text_height > 36: initialPointsize -= 5 draw.font_size = initialPointsize metric = draw.get_font_metrics(image=baseImage, text=word) # -------------------------------------------------------------- # ----------italic---------------------------------------------- if random.random() > 0.5: draw.font_style = 'italic' # -------------------------------------------------------------- # ----------underline------------------------------------------- if random.random() > 0.5: draw.text_decoration = 'underline' # -------------------------------------------------------------- # ----------gravity--------------------------------------------- draw.gravity = 'center' # -------------------------------------------------------------- # --------------shadow/border----------------------------------- if random.random() < 0.5: # shadow addx = math.ceil(random.gauss(0, 2)) addy = math.ceil(random.gauss(0, 2)) draw.fill_color = Color('black') draw.text(x=abs(int(addx)), y=abs(int(addy)), body=word) else: # border draw.stroke_color = Color('rgb('+str(color3[0])+','+str(color3[1])+','+str(color3[2])+')') draw.stroke_width = math.ceil(initialPointsize/10)-1 # -------------------------------------------------------------- # ----------print word------------------------------------------ draw.fill_color = Color('rgb('+str(color2[0])+','+str(color2[1])+','+str(color2[2])+')') draw.text(x=0, y=0, body=word) draw.draw(baseImage) # -------------------------------------------------------------- # ------------gray---------------------------------------------- baseImage.colorspace = 'gray' # -------------------------------------------------------------- print(word) baseImage.save(filename='./photo_en/'+str(count+1)+'_'+word+'.jpg')
def make_namecard(user_info_list, team_info, template): # check team directory exist(if not, make) path_team_dir = '/'.join((os.path.abspath(os.path.dirname(__file__)), 'deliverable', team_info['name'])) if not os.path.isdir(path_team_dir): os.makedirs(path_team_dir) # check icon directory exist(if not, make) path_icon_dir = '/'.join((path_team_dir, 'icon')) if not os.path.isdir(path_icon_dir): os.makedirs(path_icon_dir) # check namecard directory exist(if not, make) path_card_dir = '/'.join((path_team_dir, 'namecard')) if not os.path.isdir(path_card_dir): os.makedirs(path_card_dir) # make plain card base = Image(width=template['width'], height=template['height'], background=Color('rgb(255, 255, 255)')) frame = Drawing() frame.stroke_color = Color('rgb(0, 0, 0)') frame.fill_color = Color('rgba(255, 255, 255, 0)') frame.rectangle(left=0, top=0, width=base.width - 1, height=base.height - 1) frame(base) # draw team logo if template['logo']['display']: # check team icon exist offline(if not, get) path_logo_file = ''.join((path_team_dir, '/logo.jpg')) if not os.path.isfile(path_logo_file): icon_collector.get_image(team_info['icon'], path_logo_file) logo_draw = Image(filename=path_logo_file) logo_draw.resize(template['logo']['size'], template['logo']['size']) if template['logo']['align'] == 'left': logo_left = template['logo']['x'] elif template['logo']['align'] == 'center': logo_left = template['logo']['x'] - int( template['logo']['size'] / 2) elif template['logo']['align'] == 'right': logo_left = template['logo']['x'] - template['logo']['size'] logo_top = template['logo']['y'] base.composite(logo_draw, logo_left, logo_top) # draw team name if template['team']['display']: team_draw = Drawing() if template['team']['font'] != 'default': team_draw.font = template['team']['font'] if template['team']['size'] != 'default': team_draw.font_size = template['team']['size'] mtr_team = team_draw.get_font_metrics(base, team_info['name']) if template['team']['align'] == 'left': team_left = template['team']['x'] elif template['team']['align'] == 'center': team_left = template['team']['x'] - int(mtr_team.text_width / 2) elif template['team']['align'] == 'center': team_left = template['team']['x'] - int(mtr_team.text_width) team_top = template['team']['y'] + int(mtr_team.ascender) team_draw.text(team_left, team_top, team_info['name']) team_draw(base) #save dummy card base.save(filename=''.join((path_team_dir, '/dummy.png'))) for user in user_info_list.values(): base_clone = base.clone() # draw user icon if template['icon']['display']: # check user icon exist offline(if not, get) path_icon_file = ''.join( (path_icon_dir, '/', user['name'], '.png')) if not os.path.isfile(path_icon_file): icon_collector.get_image(user['icon'], path_icon_file) icon_draw = Image(filename=path_icon_file) icon_draw.resize(template['icon']['size'], template['icon']['size']) if template['icon']['align'] == 'left': icon_left = template['icon']['x'] elif template['icon']['align'] == 'center': icon_left = template['icon']['x'] - int( template['icon']['size'] / 2) elif template['icon']['align'] == 'right': icon_left = template['icon']['x'] - template['icon']['size'] icon_top = template['icon']['y'] base_clone.composite(icon_draw, icon_left, icon_top) # draw real name if template['real']['display']: real_draw = Drawing() if template['real']['font'] != 'default': real_draw.font = template['real']['font'] if template['real']['size'] != 'default': real_draw.font_size = template['real']['size'] user_real_name = user['real_name'] mtr_real = real_draw.get_font_metrics(base_clone, user_real_name) if mtr_real.text_width > template['real']['width']: user_real_name = user['real_name'].replace(' ', '\n') mtr_real = real_draw.get_font_metrics(base_clone, user_real_name) if template['real']['align'] == 'left': real_left = template['real']['x'] elif template['real']['align'] == 'center': real_left = template['real']['x'] - int( mtr_real.text_width / 2) elif template['real']['align'] == 'center': real_left = template['real']['x'] - int(mtr_real.text_width) real_top = template['real']['y'] + int(mtr_real.ascender) real_draw.text(real_left, real_top, user_real_name) real_draw(base_clone) # draw name if template['name']['display']: name_draw = Drawing() if template['name']['font'] != 'default': name_draw.font = template['name']['font'] if template['name']['size'] != 'default': name_draw.font_size = template['name']['size'] user_name = ''.join(('@', user['name'])) mtr_name = name_draw.get_font_metrics(base_clone, user_name) if template['name']['align'] == 'left': name_left = template['name']['x'] elif template['name']['align'] == 'center': name_left = template['name']['x'] - int( mtr_name.text_width / 2) elif template['name']['align'] == 'center': name_left = template['name']['x'] - int(mtr_name.text_width) name_top = template['name']['y'] + int(mtr_name.ascender) name_draw.text(name_left, name_top, user_name) name_draw(base_clone) # draw title if template['title']['display']: title_draw = Drawing() if template['title']['font'] != 'default': title_draw.font = template['title']['font'] if template['title']['size'] != 'default': title_draw.font_size = template['title']['size'] user_title = user['title'] mtr_title = title_draw.get_font_metrics(base_clone, user_title) if template['title']['align'] == 'left': title_left = template['title']['x'] elif template['title']['align'] == 'center': title_left = template['title']['x'] - int( mtr_title.text_width / 2) elif template['title']['align'] == 'center': title_left = template['title']['x'] - int(mtr_title.text_width) title_top = template['title']['y'] + int(mtr_title.ascender) title_draw.text(title_left, title_top, user_title) title_draw(base_clone) base_clone.save(filename=''.join((path_card_dir, '/', user['name'], '.png')))