Spaces:
Sleeping
Sleeping
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__) | |
def index(): | |
return "/handwrite?text=hello" | |
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) | |