-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
194 lines (173 loc) · 6.35 KB
/
app.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import random
from PIL import Image, ImageDraw, ImageFont
from fastapi import FastAPI
from fastapi.responses import Response, RedirectResponse
from io import BytesIO
from config import (
ENDPOINT_PREFIX,
API_VERSION,
API_TITLE,
API_DESCRIPTION,
API_DOCS_URL,
API_REDOC_URL,
API_OPENAPI_URL,
API_TERM_OF_SERVICE,
API_CONTACT_EMAIL,
API_WEBSITE,
API_NAME,
API_THUMBNAIL_MAX_HEIGHT,
API_THUMBNAIL_MAX_WIDTH,
API_THUMBNAIL_MIN_HEIGHT,
API_THUMBNAIL_MIN_WIDTH,
)
app = FastAPI(
title=API_TITLE,
description=API_DESCRIPTION,
version=API_VERSION,
docs_url=API_DOCS_URL,
redoc_url=API_REDOC_URL,
openapi_url=API_OPENAPI_URL,
terms_of_service=API_TERM_OF_SERVICE,
contact={
"name": API_NAME,
"url": API_WEBSITE,
"email": API_CONTACT_EMAIL,
},
)
def generate_gradient(
width: int, height: int, color1: tuple[int, int, int], color2: tuple[int, int, int]
) -> Image:
"""
Generates an image with a vertical gradient of the given width and height using the two given colors.
:param width: The width of the image.
:param height: The height of the image.
:param color1: The starting color of the gradient as a tuple of three integers (r, g, b).
:param color2: The ending color of the gradient as a tuple of three integers (r, g, b).
:return: An Image object containing the generated gradient.
"""
image = Image.new("RGB", (width, height))
draw = ImageDraw.Draw(image)
for y in range(height):
r = y / height
color = tuple(int(color1[i] * (1 - r) + color2[i] * r) for i in range(3))
draw.line((0, y, width, y), fill=color)
return image
def add_text_to_image(image: Image, text: str, width: int, height: int) -> Image:
"""
Adds the given text to the center of the given image.
:param image: The image to add text to.
:param text: The text to add.
:param width: The width of the image.
:param height: The height of the image.
:return: An Image object with the text added to the center.
"""
def get_font_size(text: str) -> int:
"""
Calculates the appropriate font size for the given text.
:param text: The text to calculate the font size for.
:return: The appropriate font size as an integer.
"""
length = len(text)
font_size = int(100 / (1 + 0.05 * abs(20 - length)))
return font_size
font_size = get_font_size(text)
font = ImageFont.truetype("Arial Bold.ttf", font_size, encoding="unic")
draw = ImageDraw.Draw(image)
text_width, text_height = draw.textsize(text, font=font)
position = ((width - text_width) / 2, (height - text_height) / 2)
draw.text(
position, text, fill="white", font=font, stroke_width=3, stroke_fill="black"
)
return image
@app.get(ENDPOINT_PREFIX + "{text}.png")
@app.head(ENDPOINT_PREFIX + "{text}.png", include_in_schema=False)
def get_thumbnail(
width: int = 1200,
height: int = 630,
top_color: str = None,
bottom_color: str = None,
text: str = None,
) -> Response:
"""
Generates a vertical gradient image with the given width, height, top color, bottom color, and text.
- The width and height must be integers.
- Top color and bottom color must be hex color codes.
- Text must be a string.
"""
# Make sure the width and height are within the allowed range.
if width <= API_THUMBNAIL_MIN_WIDTH or width >= API_THUMBNAIL_MAX_WIDTH:
# choose maximum width if width is too large
distance_from_max_width = abs(width - API_THUMBNAIL_MAX_WIDTH)
# choose minimum width if width is too small
distance_from_min_width = abs(width - API_THUMBNAIL_MIN_WIDTH)
if distance_from_max_width < distance_from_min_width:
width = API_THUMBNAIL_MAX_WIDTH
else:
width = API_THUMBNAIL_MIN_WIDTH
if height <= API_THUMBNAIL_MIN_HEIGHT or height >= API_THUMBNAIL_MAX_HEIGHT:
# choose maximum height if height is too large
distance_from_max_height = abs(height - API_THUMBNAIL_MAX_HEIGHT)
# choose minimum height if height is too small
distance_from_min_height = abs(height - API_THUMBNAIL_MIN_HEIGHT)
if distance_from_max_height < distance_from_min_height:
height = API_THUMBNAIL_MAX_HEIGHT
else:
height = API_THUMBNAIL_MIN_HEIGHT
# Generate a random color if no color is specified.
if top_color is None:
top_color = (
random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255),
)
else:
top_color = top_color.lstrip("#").lower()
try:
top_color = tuple(
int(top_color[i : i + 2], 16) for i in (0, 2, 4)
) # this is a tuple comprehension for converting hex to rgb
except ValueError:
top_color = (
random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255),
)
if bottom_color is None:
bottom_color = (
random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255),
)
else:
bottom_color = bottom_color.lstrip("#").lower()
try:
bottom_color = tuple(
int(bottom_color[i : i + 2], 16) for i in (0, 2, 4)
) # this is a tuple comprehension for converting hex to rgb
except ValueError:
bottom_color = (
random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255),
)
image = generate_gradient(
width,
height,
top_color,
bottom_color,
)
text = text.replace("_", " ").replace("-", " ")
image = add_text_to_image(image, text, width, height)
bytes_io = BytesIO()
image.save(bytes_io, format="PNG")
bytes_io.seek(0)
return Response(content=bytes_io.read(), media_type="image/png")
# redirect any request not defined above to the root path
@app.get("/{path:path}", include_in_schema=False)
def redirect_to_root(path: str) -> RedirectResponse:
"""
Redirects any request not defined above to the root path ("/").
:param path: The path of the request.
:return: A RedirectResponse object redirecting to the root path.
"""
return RedirectResponse(API_DOCS_URL)