File size: 1,563 Bytes
6fabbbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask , send_file , request
from io import BytesIO
from pathlib import Path
from PIL.Image import open as open_image ,Image
char_images: dict[str, Image] = {}
for i in Path("char_images").iterdir():
    char_images[i.stem] = open_image(i)
def condition(cont: str):
    back = char_images.get("zback")
    width,height = 50,0
    for letter in cont:
        if letter in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+,.-? \n":
            if letter == " ":
                letter = "zspace"
            if letter.isupper():
                letter = "c"+letter.lower()
            if letter == ",":
                letter = "coma"
            if letter == ".":
                letter = "fs"
            if letter == "?":
                letter = "que"
            if width + 150 >= back.width:
                height = height + 227
                width = 50
            if letter == "\n":
                height += 227
                width = 50
                continue
            cases = char_images.get(letter)
            back.paste(cases,(width,height))
            width += cases.width
    return back
app = Flask(__name__)
@app.get('/')
def index():
    return "/handwrite?text=hello"
@app.get('/handwrite')
def get_image():
    img = condition(request.args.get("text"))
    img_bytes = BytesIO()
    img.save(img_bytes, format='PNG')
    img_bytes.seek(0)
    return send_file(img_bytes, mimetype='image/png')

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