-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_cards.py
49 lines (37 loc) · 1.35 KB
/
generate_cards.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
import os
import requests
from io import BytesIO
from PIL import Image, ImageDraw, ImageFont
directory = os.path.dirname(os.path.realpath(__file__))
def generate_greeting(avatar_url:str, name:str):
global directory
avatar = BytesIO(requests.get(avatar_url).content)
bg_image_path = f'{directory}/cards/welcome_card.png'
im = Image.open(avatar).convert('RGBA')
im = im.resize((163,163))
font_name = 'Gilroy-SemiBold.ttf'
font = ImageFont.truetype(font=f'{directory}/fonts/{font_name}', size=25)
color_white = (255,255,255)
bigsize = (im.size[0]*3, im.size[0]*3)
mask = Image.new('L', bigsize, 0)
draw = ImageDraw.Draw(mask)
draw.ellipse((0, 0) + bigsize, fill=255)
mask = mask.resize(im.size, Image.Resampling.LANCZOS)
im.putalpha(mask)
im = im.resize((163,163))
background = Image.open(bg_image_path)
draw = ImageDraw.Draw(background)
if len(name) > 20:
name = name.split(' ')
if len(name) > 1:
name = f"{name[0]} {name[1]}"
if len(name) > 21:
name = name.split(' ')
name = f"{name[0]} {name[1][0].capitalize()}"
else:
name = str(name[0])
draw.text((189, 105), name, fill=color_white, font=font)
background.paste(im, (12,13), im)
out = BytesIO()
background.save(out, format='PNG')
return out