-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathgenImg.py
More file actions
29 lines (26 loc) · 1.05 KB
/
genImg.py
File metadata and controls
29 lines (26 loc) · 1.05 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
import requests
import json
from PIL import Image, ImageDraw, ImageFont
url = "http://localhost:4000/genImg"
headers = {'Content-Type': 'application/json'}
r = requests.get(url, headers=headers)
data = r.json()
data = json.loads(data)
print(data["tip"])
img = Image.new('RGB', (300, 300), color=(73, 109, 137))
fnt = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeSerif.ttf', 8)
fontsize = 1
fnt2 = ImageFont.truetype(
'/usr/share/fonts/truetype/freefont/FreeSerif.ttf', fontsize)
while fnt2.getsize(data["tip"])[0] < 0.5*img.size[0]:
# iterate until the text size is just larger than the criteria
fontsize += 1
fnt2 = ImageFont.truetype(
'/usr/share/fonts/truetype/freefont/FreeSerif.ttf', fontsize)
# optionally de-increment to be sure it is less than criteria
fnt2 = ImageFont.truetype(
'/usr/share/fonts/truetype/freefont/FreeSerif.ttf', fontsize)
d = ImageDraw.Draw(img)
d.text((10, 10), data["hastags"], font=fnt, fill=(255, 255, 0))
d.text((10, 40), data["tip"], font=fnt2, fill=(255, 255, 0))
img.save('pil_text_font.png')