Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,71 @@
|
|
|
|
|
| 1 |
import qrcode
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
# Create
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
)
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
# Use HTML HEX color codes for fill and background colors
|
| 18 |
-
img = qr.make_image()#fill_color="#f05a2a") (fill_color="black", back_color="white")
|
| 19 |
-
|
| 20 |
-
# Save the image
|
| 21 |
-
img.save("qrcode_corepod.png")
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import qrcode
|
| 3 |
+
import qrcode.constants
|
| 4 |
|
| 5 |
+
def get_qr_image(data: str, fill_color: str = None, back_color: str = None):
|
| 6 |
+
"""
|
| 7 |
+
Generate a QR code image from the given data and optional color parameters.
|
| 8 |
+
|
| 9 |
+
Args:
|
| 10 |
+
data (str): The data to encode in the QR code
|
| 11 |
+
fill_color (str, optional): Color of the QR code modules (default: black)
|
| 12 |
+
back_color (str, optional): Background color of the QR code (default: white)
|
| 13 |
+
|
| 14 |
+
Returns:
|
| 15 |
+
PIL.Image: Generated QR code image
|
| 16 |
+
"""
|
| 17 |
+
qr = qrcode.QRCode(
|
| 18 |
+
version=1,
|
| 19 |
+
error_correction=qrcode.constants.ERROR_CORRECT_H,
|
| 20 |
+
box_size=10,
|
| 21 |
+
border=4,
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
qr.add_data(data)
|
| 25 |
+
qr.make(fit=True)
|
| 26 |
+
|
| 27 |
+
img = qr.make_image(fill_color=fill_color, back_color=back_color)
|
| 28 |
+
return img
|
| 29 |
|
| 30 |
+
def generate_qr(url, fill_color=None, back_color=None):
|
| 31 |
+
"""
|
| 32 |
+
Wrapper function for Gradio interface to generate QR code.
|
| 33 |
+
Handles potential errors and provides default behavior.
|
| 34 |
+
|
| 35 |
+
Args:
|
| 36 |
+
url (str): URL or data to encode
|
| 37 |
+
fill_color (str, optional): Color of QR code modules
|
| 38 |
+
back_color (str, optional): Background color of QR code
|
| 39 |
+
|
| 40 |
+
Returns:
|
| 41 |
+
PIL.Image: Generated QR code image
|
| 42 |
+
"""
|
| 43 |
+
try:
|
| 44 |
+
# Validate URL/data is not empty
|
| 45 |
+
if not url:
|
| 46 |
+
raise ValueError("URL or data cannot be empty")
|
| 47 |
+
|
| 48 |
+
# Generate QR code with optional color parameters
|
| 49 |
+
return get_qr_image(url, fill_color, back_color)
|
| 50 |
+
|
| 51 |
+
except Exception as e:
|
| 52 |
+
# Error handling for Gradio
|
| 53 |
+
gr.Warning(f"Error generating QR code: {str(e)}")
|
| 54 |
+
return None
|
| 55 |
|
| 56 |
+
# Create Gradio interface
|
| 57 |
+
iface = gr.Interface(
|
| 58 |
+
fn=generate_qr,
|
| 59 |
+
inputs=[
|
| 60 |
+
gr.Textbox(label="URL or Data"),
|
| 61 |
+
gr.Textbox(label="Fill Color (optional)", placeholder="e.g., 'blue', '#FF0000'"),
|
| 62 |
+
gr.Textbox(label="Background Color (optional)", placeholder="e.g., 'white', '#FFFFFF'")
|
| 63 |
+
],
|
| 64 |
+
outputs=gr.Image(label="QR Code"),
|
| 65 |
+
title="QR Code Generator",
|
| 66 |
+
description="Generate a QR code from a URL or text with optional color customization."
|
| 67 |
)
|
| 68 |
|
| 69 |
+
# Launch the app
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|