-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
82 lines (62 loc) · 2.03 KB
/
api.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
73
74
75
76
77
78
79
80
81
82
# Pinstripe v1.0
# Image processing
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
from PIL import ImageOps
from io import BytesIO
# Web server
from flask import Flask, send_file, request, Response
app = Flask(__name__)
@app.route('/')
def img():
# Get data.
data = request.args
try:
text = request.args["subject"].upper()
except:
text = "COT"
try:
text2 = request.args["course"].upper()
except:
text2 = "3103"
try:
# Take a hexadecimal color and convert it to RGB.
hexStr = request.args["color"].replace("#","")
r = int(hexStr[0:2], 16)
g = int(hexStr[2:4], 16)
b = int(hexStr[4:6], 16)
color = (r, g, b)
except:
# The default color is #ffc904, or UCF Gold. Go Knights!
color = (255, 201, 4)
# Image size: 512x512
size = 512
# Create the image.
img = Image.new("RGBA", (size, size), (35,31,32,255))
# Load the fonts. These fonts are *not* included in this repo.
# Since the goal is to resemble UCF's official font, Gotham, I'd take a look at the font family iCeil Gotham.
normal = ImageFont.truetype("medium.otf", 106)
ultra = ImageFont.truetype("ultra.otf", 126)
d = ImageDraw.Draw(img)
# Draw pinstripe
pos = 150
d.line((-(pos), (pos)) + ((pos), -(pos)), fill=color, width=300)
# Licensing note: The text-centering algorithm below is adapted from https://stackoverflow.com/a/55774238
# Draw ultra text
w, h = d.textsize(text, font=ultra)
d.text(((size-w)/2, ((size-h)/2)-60), text, font=ultra, fill=(255,255,255,255))
# Draw normal text
w, h = d.textsize(text2, font=normal)
d.text(((size-w)/2, ((size-h)/2)+60), text2, font=normal, fill=(255,255,255,255))
# Save to memory.
file = BytesIO()
img.save(file, "png")
file.seek(0)
return send_file(
file,
attachment_filename="icon.png",
mimetype="image/png"
)
if __name__ == '__main__':
app.run()