Spaces:
Runtime error
Runtime error
Commit ·
e992434
1
Parent(s): b823125
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,72 +1,58 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
import qrcode
|
| 4 |
|
| 5 |
-
def
|
|
|
|
| 6 |
qr = qrcode.QRCode(
|
| 7 |
version=1,
|
| 8 |
-
error_correction=qrcode.constants.
|
| 9 |
box_size=10,
|
| 10 |
border=4,
|
| 11 |
)
|
| 12 |
-
|
|
|
|
| 13 |
qr.add_data(url)
|
| 14 |
qr.make(fit=True)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
qr_image = qr_image.convert("RGB")
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
border_radius = max(1, min(border_radius, int(width/2), int(height/2)))
|
| 35 |
-
mask = Image.new('L', (width, height), 0)
|
| 36 |
-
draw = ImageDraw.Draw(mask)
|
| 37 |
-
draw.ellipse((0, 0, 2*border_radius, 2*border_radius), fill=255)
|
| 38 |
-
draw.ellipse((width-2*border_radius, 0, width, 2*border_radius), fill=255)
|
| 39 |
-
draw.ellipse((0, height-2*border_radius, 2*border_radius, height), fill=255)
|
| 40 |
-
draw.ellipse((width-2*border_radius, height-2*border_radius, width, height), fill=255)
|
| 41 |
-
alpha = Image.new('L', (width, height), int(255 * 0.5))
|
| 42 |
-
qr_image.paste(alpha, (0, 0), mask)
|
| 43 |
|
| 44 |
-
|
| 45 |
-
logo_image = Image.open(logo).convert("RGBA")
|
| 46 |
-
qr_image.paste(logo_image, (int((qr_image.size[0] - logo_image.size[0]) / 2), int((qr_image.size[1] - logo_image.size[1]) / 2)), logo_image)
|
| 47 |
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
|
| 52 |
-
|
| 53 |
-
fn=
|
| 54 |
-
inputs=
|
| 55 |
-
|
| 56 |
-
gr.ColorPicker(label="Color", default="#000000"),
|
| 57 |
-
gr.Checkbox(label="Gradient", default=False),
|
| 58 |
-
gr.ColorPicker(label="Gradient Colors", default=["#FFFFFF", "#000000"], nargs=2),
|
| 59 |
-
gr.Number(label="Border Radius", default=0),
|
| 60 |
-
gr.inputs.File(label="Logo")
|
| 61 |
-
],
|
| 62 |
-
outputs="image",
|
| 63 |
title="QR Code Generator",
|
| 64 |
-
description="
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
["twitter.com", "#00FF00", False, ["#FFFFFF", "#000000"], 0, "logo.png"],
|
| 68 |
-
["huggingface.co", "#0000FF", True, ["#FF00FF", "#00FFFF"], 15, "logo.png"]
|
| 69 |
-
]
|
| 70 |
-
)
|
| 71 |
-
|
| 72 |
-
iface.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import qrcode
|
| 3 |
|
| 4 |
+
def generate_qr_code(url, gradient_color1, gradient_color2, logo, qr_color):
|
| 5 |
+
# Create QR code instance
|
| 6 |
qr = qrcode.QRCode(
|
| 7 |
version=1,
|
| 8 |
+
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
| 9 |
box_size=10,
|
| 10 |
border=4,
|
| 11 |
)
|
| 12 |
+
|
| 13 |
+
# Add URL to the QR code
|
| 14 |
qr.add_data(url)
|
| 15 |
qr.make(fit=True)
|
| 16 |
|
| 17 |
+
# Create the QR code image
|
| 18 |
+
qr_image = qr.make_image(fill_color=qr_color, back_color="#ffffff") # Set QR code color here
|
| 19 |
+
|
| 20 |
+
# Add gradient if colors are provided
|
| 21 |
+
if gradient_color1 and gradient_color2:
|
| 22 |
qr_image = qr_image.convert("RGB")
|
| 23 |
+
qr_image = qr_image.resize((500, 500))
|
| 24 |
+
|
| 25 |
+
gradient = Image.new("RGB", qr_image.size)
|
| 26 |
+
grad = ImageDraw.Gradient(grad.size, "LR", gradient_color1, gradient_color2)
|
| 27 |
+
qr_image = Image.blend(qr_image, gradient, 0.5)
|
| 28 |
+
|
| 29 |
+
# Add logo if provided
|
| 30 |
+
if logo:
|
| 31 |
+
logo_image = Image.open(logo).convert("RGBA")
|
| 32 |
+
qr_image.paste(logo_image, (125, 125), logo_image)
|
| 33 |
|
| 34 |
+
# Save the QR code to a byte stream
|
| 35 |
+
qr_byte_stream = io.BytesIO()
|
| 36 |
+
qr_image.save(qr_byte_stream, format="PNG")
|
| 37 |
+
qr_byte_stream.seek(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
return qr_byte_stream.read()
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
inputs = [
|
| 42 |
+
gr.inputs.Textbox(label="URL to Site"),
|
| 43 |
+
gr.inputs.ColorPicker(label="Gradient Color 1 (Optional)"),
|
| 44 |
+
gr.inputs.ColorPicker(label="Gradient Color 2 (Optional)"),
|
| 45 |
+
gr.inputs.Image(label="Logo (Optional)"),
|
| 46 |
+
gr.inputs.ColorPicker(label="QR Code Color"),
|
| 47 |
+
]
|
| 48 |
|
| 49 |
+
outputs = gr.outputs.Image(type="bytes")
|
| 50 |
|
| 51 |
+
gr.Interface(
|
| 52 |
+
fn=generate_qr_code,
|
| 53 |
+
inputs=inputs,
|
| 54 |
+
outputs=outputs,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
title="QR Code Generator",
|
| 56 |
+
description="Generate QR codes with customizable options",
|
| 57 |
+
theme="huggingface",
|
| 58 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|