File size: 7,307 Bytes
f8c7305
 
7de72ad
f8c7305
7de72ad
c2743bd
7de72ad
 
a9dedf0
c2743bd
 
f8c7305
23e5a2c
a9dedf0
23e5a2c
 
a9dedf0
f8c7305
f011119
76a257d
 
 
 
 
f011119
f8c7305
f011119
f8c7305
 
 
a34a680
f8c7305
 
 
a34a680
f8c7305
a34a680
f8c7305
a34a680
 
 
f8c7305
a34a680
 
 
f8c7305
 
 
 
 
 
 
 
a34a680
f8c7305
 
 
 
f011119
 
f8c7305
 
 
 
 
 
 
 
 
 
 
f011119
f8c7305
 
f011119
f8c7305
 
 
f011119
f8c7305
 
 
f011119
 
f8c7305
f011119
7de72ad
 
 
f011119
7de72ad
 
 
f011119
 
7de72ad
 
 
f011119
 
7de72ad
 
f011119
 
7de72ad
 
f011119
 
7de72ad
a9dedf0
f011119
7de72ad
 
f011119
7de72ad
 
 
a9dedf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f011119
7de72ad
f011119
 
a9dedf0
 
7de72ad
a9dedf0
 
 
 
 
 
 
 
 
 
 
 
 
 
bf8ade6
a9dedf0
f011119
a9dedf0
7de72ad
f011119
 
a9dedf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf8ade6
76a257d
 
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
from PIL import Image, ImageDraw, ImageFont, ImageEnhance
from pilmoji import Pilmoji
from flask import Flask, request, send_file
import textwrap
import requests
import warnings
import base64
import io
import os

warnings.simplefilter("ignore")

BASE_GD_IMAGE = Image.open("images/base-gd.png")
BASE_FLIPPED_IMAGE = Image.open("images/base-gd-flipped.png")
BASE_IMAGE = Image.open("images/base.png")
MPLUS_FONT = ImageFont.truetype("fonts/MPLUSRounded1c-Regular.ttf", size=16)
branding = "Change this to the text of your choice"


def draw_text(
    im, ofs, string, font="fonts/MPLUSRounded1c-Regular.ttf", size=16,
    color=(0, 0, 0, 255), split_len=None, padding=4, auto_expand=False,
    disable_dot_wrap=False
):

    draw = ImageDraw.Draw(im)
    fontObj = ImageFont.truetype(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 == "、" or char == ",":
                pure_lines.append(l + ("、" if char == "、" else ","))
                l = ""
                pos += 1
            elif char == "。" or char == ".":
                pure_lines.append(l + ("。" if char == "。" else "."))
                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(textwrap.wrap(line, width=split_len))

    dy = 0

    draw_lines = []

    for line in lines:
        tsize = fontObj.getsize(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:
            p.text((dl[0], (adj_y + dl[1])), dl[2], font=fontObj, fill=color)

    real_y = ofs[1] + adj_y + dy

    return (0, dy, real_y)


def make(name, tag, 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 = draw_text(img, (890, 270), content, size=45, color=(
        255, 255, 255, 255), split_len=16, auto_expand=True)

    name_y = tsize_t[2] + 40
    tsize_name = draw_text(img, (890, name_y), f"{name}#{tag}", size=25, color=(
        255, 255, 255, 255), split_len=25, disable_dot_wrap=True)

    id_y = name_y + tsize_name[1] + 4
    tsize_id = draw_text(img, (890, id_y), id, size=18, color=(
        180, 180, 180, 255), split_len=45, disable_dot_wrap=True)

    tx.text((1125, 694), branding,
            font=MPLUS_FONT, fill=(120, 120, 120, 255))

    file = io.BytesIO()
    img.save(file, format="PNG", quality=95)
    file.seek(0)
    return file

def colourmake(name, tag, id, content, icon):
    img = BASE_IMAGE.copy()

    icon = Image.open(io.BytesIO(requests.get(icon).content))
    icon = icon.resize((720, 720), Image.LANCZOS)
    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 = draw_text(img, (890, 270), content, size=45, color=(
        255, 255, 255, 255), split_len=16, auto_expand=True)

    name_y = tsize_t[2] + 40
    tsize_name = draw_text(img, (890, name_y), f"{name}#{tag}", size=25, color=(
        255, 255, 255, 255), split_len=25, disable_dot_wrap=True)

    id_y = name_y + tsize_name[1] + 4
    tsize_id = draw_text(img, (890, id_y), id, size=18, color=(
        180, 180, 180, 255), split_len=45, disable_dot_wrap=True)

    tx.text((1125, 694), branding,
            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, tag, 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_FLIPPED_IMAGE, (0, 0), BASE_FLIPPED_IMAGE)

    tx = ImageDraw.Draw(img)

    tsize_t = draw_text(img, (390, 270), content, size=45, color=(
        255, 255, 255, 255), split_len=16, auto_expand=True)

    name_y = tsize_t[2] + 40
    tsize_name = draw_text(img, (390, name_y), f"{name}#{tag}", size=25, color=(
        255, 255, 255, 255), split_len=25, disable_dot_wrap=True)

    id_y = name_y + tsize_name[1] + 4
    tsize_id = draw_text(img, (390, id_y), id, size=18, color=(
        180, 180, 180, 255), split_len=45, disable_dot_wrap=True)

    tx.text((15, 694), branding,
            font=MPLUS_FONT, fill=(120, 120, 120, 255))

    file = io.BytesIO()
    img.save(file, format="PNG", quality=95)
    file.seek(0)
    return file



app = Flask(__name__)


@app.route("/original", methods=["GET"])
def original():
    res = make(
        request.args.get("name") or "SAMPLE",
        request.args.get("tag") or "1234",
        request.args.get("id") or "0000000000000000000",
        request.args.get("content") or "This isn't a very good quote unless you say something",
        request.args.get(
            "icon") or "https://cdn.mikn.dev/MikanBot.png"
    )
    return send_file(res, mimetype="image/png")

@app.route("/colour", methods=["GET"])
def colour():
    res = colourmake(
        request.args.get("name") or "SAMPLE",
        request.args.get("tag") or "1234",
        request.args.get("id") or "0000000000000000000",
        request.args.get("content") or "This isn't a very good quote unless you say something",
        request.args.get(
            "icon") or "https://cdn.mikn.dev/MikanBot.png"
    )
    return send_file(res, mimetype="image/png")

@app.route("/reverse", methods=["GET"])
def reverse():
    res = reversemake(
        request.args.get("name") or "SAMPLE",
        request.args.get("tag") or "1234",
        request.args.get("id") or "0000000000000000000",
        request.args.get("content") or "This isn't a very good quote unless you say something",
        request.args.get(
            "icon") or "https://cdn.mikn.dev/MikanBot.png"
    )
    return send_file(res, mimetype="image/png")

@app.route("/", methods=["GET"])
def main():
    return 'API URL: https://miq-api.mikanbot.com/<br><br>Endpoints<br>/original Original B&W MiaQ image<br>/colour MiaQ image with coloured icon<br>/reverse MiaQ image with flipped icon position<br><br>Query Parameters<br>name: Username<br>tag: Tag<br>id: User ID<br>icon: Icon URL<br>content: Message Content<br><br>Host your own API here! (https://github.com/maamokun/miq-api)<br>Original code from Taka005 (https://github.com/Taka005/miq)'


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=3000)