File size: 2,005 Bytes
86dbcb0 66d0ec4 86dbcb0 205aa1f 86dbcb0 66d0ec4 86dbcb0 66d0ec4 86dbcb0 66d0ec4 86dbcb0 66d0ec4 | 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | # app.py
# pip install gradio qrcode[pil]
import gradio as gr
from PIL import Image
import qrcode
from qrcode.constants import ERROR_CORRECT_L, ERROR_CORRECT_M, ERROR_CORRECT_Q, ERROR_CORRECT_H
EC_MAP = {
"L (7%)": ERROR_CORRECT_L,
"M (15%)": ERROR_CORRECT_M,
"Q (25%)": ERROR_CORRECT_Q,
"H (30%)": ERROR_CORRECT_H,
}
def make_qr(
text: str,
error_level: str,
box_size: int,
border: int,
fill_color: str,
back_color: str,
):
if not text or not text.strip():
return Image.new("RGB", (256, 256), color="white")
qr = qrcode.QRCode(
version=None, # let library choose optimal size
error_correction=EC_MAP[error_level],
box_size=box_size,
border=border,
)
qr.add_data(text.strip())
qr.make(fit=True)
img = qr.make_image(fill_color=fill_color, back_color=back_color)
return img.convert("RGB")
with gr.Blocks(title="Text → QR Code") as demo:
gr.Markdown("# Text → QR Code")
with gr.Row():
with gr.Column():
text = gr.Textbox(label="Text or URL", placeholder="Type or paste your text here…", lines=4)
error_level = gr.Dropdown(
choices=list(EC_MAP.keys()),
value="M (15%)",
label="Error correction",
)
box_size = gr.Slider(4, 20, value=10, step=1, label="Box size (pixel per module)")
border = gr.Slider(1, 10, value=4, step=1, label="Border (modules)")
fill_color = gr.ColorPicker(value="#000000", label="QR color")
back_color = gr.ColorPicker(value="#FFFFFF", label="Background color")
btn = gr.Button("Generate", variant="primary")
with gr.Column():
out = gr.Image(type="pil", label="QR Code", height=400)
btn.click(
fn=make_qr,
inputs=[text, error_level, box_size, border, fill_color, back_color],
outputs=out,
)
if __name__ == "__main__":
demo.launch()
|