-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyconBuilder.py
72 lines (71 loc) · 3 KB
/
PyconBuilder.py
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
#Creates PNG images from text (string) input.
#Use: For multiple lines, seperate words with spaces
#FONT: Arimo - http://www.google.com/webfonts/specimen/Arimo
import ImageFont, ImageDraw, Image, os
def createImageFromText(txt,base_image,font_path,split=""):
# split text input into multiple words in a list
txt = txt.split(split)
#open background image
image = Image.open(base_image)
#create a drawable image
draw = ImageDraw.Draw(image)
#set initial font size to 1 (smallest)
fontsize = 1
# set the precentage of base image text should occupy - .9 provides good border
img_fraction = 0.90
#creat intial font using fontsize of 1
font = ImageFont.truetype(font_path, fontsize)
#set up image width and height variables to be used later
img_w, img_h = image.size
#how large can the text
max_width = img_fraction*image.size[0]
max_height = img_fraction*(image.size[1]/len(txt)+1)
#if statement - first block for single words
if len(txt) < 2:
#set text to be the first element in txt list
txt = txt[0]
#while the font fits the width of the image space - increase font
while font.getsize(txt)[0] < max_width:
fontsize += 1
font = ImageFont.truetype(font_path, fontsize)
#reduce fontsize by 1 to ensure it fits nicely in space
fontsize -= 1
#create the font with new found fontsize
font = ImageFont.truetype(font_path, fontsize)
#create variables for text width and height
text_w, text_h = font.getsize(txt)
#draw the text on the image, place it at the center vertically and horizontally
draw.text(((img_w - text_w)/2, (img_h/2-text_h/2)), txt, font=font,fill ="#000000")
#save image
image.save(txt[0]+'.png')
#print out confirmation to cmdline
print(txt[0]+'.png created successfully')
# else block assumes multiline text (space delineated)
else:
#a list to collect font sizes of each word in txt
fontsizes = []
# for every word, find the max font size given image constraints
for item in txt:
fontsize = 1
font = ImageFont.truetype(font_path, fontsize)
while (font.getsize(item)[0] < max_width) and (font.getsize(item)[1] < max_height):
fontsize += 1
font = ImageFont.truetype(font_path, fontsize)
#reduce fontsize by 1 to ensure it fits nicely in space
fontsize -= 1
#append 'found' font size to list to find min (next)
fontsizes.append(fontsize)
#set the font to a font with the smallest (largest) possible for all words in txt
font = ImageFont.truetype(font_path, min(fontsizes))
# set the variables for the width and height of the smallest (largest) word
text_w, text_h = font.getsize(txt[fontsizes.index(min(fontsizes))])
# draw each word in a specific place
for item in range(len(txt)):
text_w, t = font.getsize(txt[item])
w = (img_w - text_w)/2
h = (item+1)*(img_h/(len(txt)+1))-(text_h/2)
draw.text((w, h), txt[item], font=font,fill ="#000000")
#save image
image.save("".join(txt)+'.png')
#print out confirmation to cmdline
print("".join(txt)+'.png created successfully')