kac/server.py

31 lines
779 B
Python

from fastapi import FastAPI, File, UploadFile
import StarTSPImage
from PIL import Image
app = FastAPI()
@app.post("/upload")
def upload(file: UploadFile = File(...)):
try:
contents = file.file.read()
with open("image.png", 'wb') as f:
f.write(contents)
except Exception:
return {"message": "There was an error uploading the file"}
finally:
file.file.close()
fr=Image.open("frame.png")
im=Image.open("image.png")
im = im.resize((432, 432), Image.Resampling.BOX)
fr.paste(im, (34,34), im)
#fr.show()
raster = StarTSPImage.imageToRaster(fr, cut=True)
printer = open('/dev/usb/lp0', "wb")
printer.write(raster)
return {"message": f"Successfully uploaded {file.filename}"}