qrcode / app.py
sathish2352's picture
Update app.py
84f6df9 verified
raw
history blame contribute delete
719 Bytes
from fastapi import FastAPI, HTTPException
from fastapi.responses import StreamingResponse
import qrcode
import io
app = FastAPI()
@app.post("/generate_qr/")
async def generate_qr(text: str):
if not text or not text.strip():
raise HTTPException(status_code=400, detail="Text input is empty")
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
qr.add_data(text.strip())
qr.make(fit=True)
img = qr.make_image(fill_color="#0057D8", back_color="white").convert("RGB")
buf = io.BytesIO()
img.save(buf, format="PNG")
buf.seek(0)
return StreamingResponse(buf, media_type="image/png")