File size: 11,437 Bytes
0d3f763 0ec8a97 cbb71bd a00db90 cbb71bd f492eaf a00db90 0d3f763 e46bfee 83d2c10 ed33368 e46bfee 9cbec7a e46bfee 9cbec7a df183e7 9cbec7a 0ec8a97 2a81db0 9cbec7a 2a81db0 9cbec7a 0d3f763 2523c7e 0d3f763 2523c7e 0d3f763 2523c7e 0d3f763 9cbec7a 2523c7e 0d3f763 2523c7e 0d3f763 f492eaf 2a81db0 0d3f763 0ec8a97 0d3f763 9cbec7a 2a81db0 0d3f763 2a81db0 9cbec7a 0d3f763 9cbec7a 0d3f763 2a81db0 925606b cbb71bd 2a81db0 cbb71bd 2a81db0 cbb71bd f492eaf cbb71bd fad3690 cbb71bd eeaf427 cbb71bd 0ec8a97 cbb71bd 2a81db0 cbb71bd 925606b 087edcb 51b4d27 087edcb f492eaf 087edcb fad3690 087edcb eeaf427 087edcb 0ec8a97 087edcb 925606b 087edcb 83d2c10 087edcb f492eaf 087edcb fad3690 087edcb eeaf427 087edcb a38c891 ed33368 3a08c3d ed33368 b44580b 3a08c3d f492eaf 3a08c3d fad3690 3a08c3d eeaf427 3a08c3d b44580b ed33368 f492eaf ed33368 fad3690 ed33368 eeaf427 ed33368 0ec8a97 a38c891 925606b a38c891 3a08c3d a38c891 f492eaf a38c891 fad3690 a38c891 eeaf427 a38c891 087edcb 0ec8a97 03d1f4a 0ec8a97 03d1f4a 0ec8a97 03d1f4a 0ec8a97 03d1f4a 0ec8a97 03d1f4a 0ec8a97 03d1f4a 0ec8a97 a38c891 430587b 0ec8a97 | 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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | from PIL import Image, ImageDraw, ImageFont, ImageEnhance
from pilmoji import Pilmoji
# from flask import Flask, request, send_file
from fastapi import FastAPI, Request, Query
from fastapi.responses import StreamingResponse
import uvicorn
import requests
import warnings
import io
from wrap import fw_wrap
warnings.simplefilter("ignore")
BASE_GD_IMAGE = Image.open("images/base-gd.png")
BASE_RV_IMAGE = Image.open("images/base-gd-rv.png")
BASE_GD_W_IMAGE = Image.open("images/base-gd-w.png")
BASE_RV_W_IMAGE = Image.open("images/base-gd-w-rv.png")
BASE_IMAGE = Image.open("images/base.png")
if BASE_IMAGE.mode != "RGBA":
BASE_IMAGE = BASE_IMAGE.convert("RGBA")
MPLUS_FONT = ImageFont.truetype("fonts/MPLUSRounded1c-Regular.ttf", size=16)
MPLUS_FONTBOLD = ImageFont.truetype("fonts/MPLUSRounded1c-Bold.ttf", size=16)
# BRAND = "http://hisoka.net\nhttp://s.id/MaiSakurajima"
# print(MPLUS_FONT.get_variation_names())
BRAND = "http://s.id/MaiSakurajima"
def getsize(font, text):
left, top, right, bottom = font.getbbox(text)
return right - left, bottom
from PIL import ImageFont, ImageDraw
from pilmoji import Pilmoji
def drawText(im, ofs, string, font="fonts/MPLUSRounded1c-Regular.ttf", bold_font="fonts/MPLUSRounded1c-Bold.ttf", size=16, color=(0, 0, 0, 255), split_len=None, padding=4, disable_dot_wrap=False):
v = ImageDraw.Draw(im)
fontObj = ImageFont.truetype(font, size=size)
boldFontObj = ImageFont.truetype(bold_font, size=size)
pure_lines = []
pos = 0
l = ""
if not disable_dot_wrap:
for char in string:
if char == "\n":
pure_lines.append(l)
l = ""
pos += 1
elif char in ["、", ",", "。", "."]:
pure_lines.append(l + char)
l = ""
pos += 1
else:
l += char
pos += 1
if l:
pure_lines.append(l)
else:
pure_lines = string.split("\n")
lines = []
for line in pure_lines:
lines.extend(fw_wrap(line, width=split_len))
dy = 0
draw_lines = []
for line in lines:
tsize = getsize(fontObj, line)
ofs_y = ofs[1] + dy
t_height = tsize[1]
x = int(ofs[0] - (tsize[0] / 2))
draw_lines.append((x, ofs_y, line))
ofs_y += t_height + padding
dy += t_height + padding
adj_y = -30 * (len(draw_lines) - 1)
for dl in draw_lines:
with Pilmoji(im) as p:
parts = dl[2].split('*') # * for bold
xPos = dl[0]
yPos = adj_y + dl[1]
for i, part in enumerate(parts):
if i % 2 == 1:
p.text((xPos, yPos), part, font=boldFontObj, fill=color)
xPos += v.textlength(part, font=boldFontObj)
else:
subparts = part.split('_') # _ for italic
for j, subpart in enumerate(subparts):
if j % 2 == 1:
text_width, text_height = getsize(fontObj, subpart)
padding = 15
italic_text_image = Image.new("RGBA", (text_width + padding * 2, text_height + padding * 2), (0, 0, 0, 0))
italic_draw = ImageDraw.Draw(italic_text_image)
italic_draw.text((padding, padding), subpart, font=fontObj, fill=color)
slant_amount = 0.2
italic_text_image = italic_text_image.transform(
italic_text_image.size,
Image.AFFINE,
(1, slant_amount, 0, 0, 1, 0),
resample=Image.BICUBIC
)
im.alpha_composite(italic_text_image, dest=(int(xPos), int(yPos - padding)))
xPos += text_width + int(slant_amount * size)
else:
strikethrough_parts = subpart.split('~')
for k, s_part in enumerate(strikethrough_parts):
if k % 2 == 1:
p.text((xPos, yPos), s_part, font=fontObj, fill=color)
text_width = v.textlength(s_part, font=fontObj)
line_y = yPos + text_height // 2 # Middle of the text
v.line((xPos, line_y, xPos + text_width, line_y), fill=color, width=1)
xPos += text_width
else:
p.text((xPos, yPos), s_part, font=fontObj, fill=color)
xPos += v.textlength(s_part, font=fontObj)
real_y = ofs[1] + adj_y + dy
return (0, dy, real_y)
def make(name, id, content, icon):
img = BASE_IMAGE.copy()
icon = Image.open(io.BytesIO(requests.get(icon).content))
icon = icon.resize((720, 720), Image.LANCZOS)
icon = icon.convert("L")
icon_filtered = ImageEnhance.Brightness(icon)
img.paste(icon_filtered.enhance(0.7), (0, 0))
img.paste(BASE_GD_IMAGE, (0, 0), BASE_GD_IMAGE)
tx = ImageDraw.Draw(img)
tsize_t = drawText(img, (890, 270), content, size=55, color=(255, 255, 255, 255), split_len=20)
name_y = tsize_t[2] + 40
tsize_name = drawText(img, (890, name_y), f"@{name}", size=28, color=(255, 255, 255, 255), split_len=25, disable_dot_wrap=True)
id_y = name_y + tsize_name[1] + 4
drawText(img, (890, id_y), id, size=18, color=(180, 180, 180, 255), split_len=45, disable_dot_wrap=True)
tx.text((1070, 694), BRAND, font=MPLUS_FONT, fill=(120, 120, 120, 255))
file = io.BytesIO()
img.save(file, format="PNG", quality=95)
file.seek(0)
return file
def colorMake(name, id, content, icon):
img = BASE_IMAGE.copy()
icon = Image.open(io.BytesIO(requests.get(icon).content))
icon = icon.resize((720, 720), Image.LANCZOS)
img.paste(icon, (0, 0))
img.paste(BASE_GD_IMAGE, (0, 0), BASE_GD_IMAGE)
tx = ImageDraw.Draw(img)
tsize_t = drawText(img, (890, 270), content, size=55, color=(255, 255, 255, 255), split_len=20)
name_y = tsize_t[2] + 40
tsize_name = drawText(img, (890, name_y), f"@{name}", size=28, color=(255, 255, 255, 255), split_len=25, disable_dot_wrap=True)
id_y = name_y + tsize_name[1] + 4
drawText(img, (890, id_y), id, size=18, color=(180, 180, 180, 255), split_len=45, disable_dot_wrap=True)
tx.text((1070, 694), BRAND, font=MPLUS_FONT, fill=(120, 120, 120, 255))
file = io.BytesIO()
img.save(file, format="PNG", quality=95)
file.seek(0)
return file
def reverseMake(name, id, content, icon):
img = BASE_IMAGE.copy()
icon = Image.open(io.BytesIO(requests.get(icon).content))
icon = icon.resize((720, 720), Image.LANCZOS)
icon = icon.convert("L")
icon_filtered = ImageEnhance.Brightness(icon)
img.paste(icon_filtered.enhance(0.7), (570, 0))
img.paste(BASE_RV_IMAGE, (0, 0), BASE_RV_IMAGE)
tx = ImageDraw.Draw(img)
tsize_t = drawText(img, (390, 270), content, size=55, color=(255, 255, 255, 255), split_len=20)
name_y = tsize_t[2] + 40
tsize_name = drawText(img, (390, name_y), f"@{name}", size=28, color=(255, 255, 255, 255), split_len=25, disable_dot_wrap=True)
id_y = name_y + tsize_name[1] + 4
drawText(img, (390, id_y), id, size=18, color=(180, 180, 180, 255), split_len=45, disable_dot_wrap=True)
tx.text((6, 694), BRAND, font=MPLUS_FONT, fill=(120, 120, 120, 255))
file = io.BytesIO()
img.save(file, format="PNG", quality=95)
file.seek(0)
return file
def reverseColorMake(name, id, content, icon):
img = BASE_IMAGE.copy()
icon = Image.open(io.BytesIO(requests.get(icon).content))
icon = icon.resize((720, 720), Image.LANCZOS)
img.paste(icon, (570, 0))
img.paste(BASE_RV_IMAGE, (0, 0), BASE_RV_IMAGE)
tx = ImageDraw.Draw(img)
tsize_t = drawText(img, (390, 270), content, size=55, color=(255, 255, 255, 255), split_len=20)
name_y = tsize_t[2] + 40
tsize_name = drawText(img, (390, name_y), f"@{name}", size=28, color=(255, 255, 255, 255), split_len=25, disable_dot_wrap=True)
id_y = name_y + tsize_name[1] + 4
drawText(img, (390, id_y), id, size=18, color=(180, 180, 180, 255), split_len=45, disable_dot_wrap=True)
tx.text((6, 694), BRAND, font=MPLUS_FONT, fill=(120, 120, 120, 255))
file = io.BytesIO()
img.save(file, format="PNG", quality=95)
file.seek(0)
return file
def whiteMake(name, id, content, icon):
img = BASE_IMAGE.copy()
icon = Image.open(io.BytesIO(requests.get(icon).content)).convert("RGBA")
icon = icon.resize((720, 720), Image.LANCZOS)
img.paste(icon, (0, 0), icon)
img.paste(BASE_GD_W_IMAGE, (0, 0), BASE_GD_W_IMAGE)
tx = ImageDraw.Draw(img)
tsize_t = drawText(img, (890, 270), content, size=55, color=(0, 0, 0, 0), split_len=20)
name_y = tsize_t[2] + 40
tsize_name = drawText(img, (890, name_y), f"@{name}", size=28, color=(0, 0, 0, 0), split_len=25, disable_dot_wrap=True)
id_y = name_y + tsize_name[1] + 4
drawText(img, (890, id_y), id, size=18, color=(90, 90, 90, 255), split_len=45, disable_dot_wrap=True)
tx.text((1070, 694), BRAND, font=MPLUS_FONT, fill=(120, 120, 120, 255))
file = io.BytesIO()
img.save(file, format="PNG", quality=95)
file.seek(0)
return file
def reverseWhiteMake(name, id, content, icon):
img = BASE_IMAGE.copy()
icon = Image.open(io.BytesIO(requests.get(icon).content)).convert("RGBA")
icon = icon.resize((720, 720), Image.LANCZOS)
img.paste(icon, (570, 0), icon)
img.paste(BASE_RV_W_IMAGE, (0, 0), BASE_RV_W_IMAGE)
tx = ImageDraw.Draw(img)
tsize_t = drawText(img, (390, 270), content, size=55, color=(0, 0, 0, 0), split_len=20)
name_y = tsize_t[2] + 40
tsize_name = drawText(img, (390, name_y), f"@{name}", size=28, color=(0, 0, 0, 0), split_len=25, disable_dot_wrap=True)
id_y = name_y + tsize_name[1] + 4
drawText(img, (390, id_y), id, size=18, color=(90, 90, 90, 255), split_len=45, disable_dot_wrap=True)
tx.text((6, 694), BRAND, font=MPLUS_FONT, fill=(110, 110, 110, 255))
file = io.BytesIO()
img.save(file, format="PNG", quality=95)
file.seek(0)
return file
app = FastAPI()
@app.get("/")
async def main(
type: str = Query(None),
name: str = Query("SAMPLE"),
id: str = Query(""),
content: str = Query("Make it a Quote"),
icon: str = Query("https://cdn.discordapp.com/embed/avatars/0.png")
):
if type == "color":
image_io = colorMake(name, id, content, icon)
elif type == "reverse":
image_io = reverseMake(name, id, content, icon)
elif type == "reverseColor":
image_io = reverseColorMake(name, id, content, icon)
elif type == "white":
image_io = whiteMake(name, id, content, icon)
elif type == "reverseWhite":
image_io = reverseWhiteMake(name, id, content, icon)
else:
image_io = make(name, id, content, icon)
image_io.seek(0)
return StreamingResponse(image_io, media_type="image/png")
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=3000) |