Download app.py from GreatClap/qrcoder: direct link, hf CLI and curl.
- Browser
- Download file 2.01 kB
-
https://huggingface.co/spaces/GreatClap/qrcoder/resolve/main/app.py
- Command line
-
hf download hf://spaces/GreatClap/qrcoder/app.py
-
curl -L -o app.py https://huggingface.co/spaces/GreatClap/qrcoder/resolve/main/app.py
2.01 kB
| # 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() | |