-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
34 lines (24 loc) · 834 Bytes
/
main.py
File metadata and controls
34 lines (24 loc) · 834 Bytes
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
from fastapi import FastAPI, UploadFile, File
from fastapi.responses import JSONResponse
from PIL import Image
import io
from escpos.printer import Usb
app = FastAPI()
p = Usb(0x04b8, 0x0e28, profile="TM-T20II")
@app.post("/print-image")
async def print_image(file: UploadFile = File(...)):
try:
# Read the image file
image_data = await file.read()
image = Image.open(io.BytesIO(image_data))
# Open the printer connection
p.open()
# Print the image
p.image(image, center=True)
# Cut the paper
p.cut()
# Close the printer connection
p.close()
return JSONResponse(content={"message": "Printed successfully!"}, status_code=200)
except Exception as e:
return JSONResponse(content={"error": str(e)}, status_code=500)