-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageGen.py
More file actions
33 lines (24 loc) · 1 KB
/
ImageGen.py
File metadata and controls
33 lines (24 loc) · 1 KB
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
import imgkit
from PIL import Image
def findMultiplier(letters):
return 8.6 - 2.75 * pow(letters, 0.189)
def findFontSize(pixels, numOfLetters):
return findMultiplier(numOfLetters) * pow(pixels, 0.5)
def ImageGen(text, styleClass):
# dynamic font size (DON'T CHANGE ANY NUMBERS)
numOfLetters = len(text)
fontSize = findFontSize(600, numOfLetters)
htmlString = '<html><head><link rel="stylesheet" href="test_fonts.css">\
</head><body style="margin:0"><div class="%s" style="text-align:center;\
word-break: break-word; width: 504px; padding:0; margin:0; font-size:%fpx">' % (styleClass, fontSize)\
+ text + '</div></body></html>'
# Image options
options = {
'transparent': None,
'crop-w': 512,
'xvfb': ''
}
css = 'styles.css'
imgkit.from_string(htmlString, 'images/test.png', css=css, options=options)
foo = Image.open('images/test.png')
foo.save('images/test.png', optimize=True)