Spaces:
Runtime error
Runtime error
Commit ·
cee8667
1
Parent(s): 5530e78
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,77 +1,69 @@
|
|
| 1 |
-
import qrcode
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 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 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
if border_radius > 0:
|
| 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 |
-
inputs
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
output = gr.outputs.Image(label="QR Code")
|
| 69 |
-
|
| 70 |
-
examples = [
|
| 71 |
-
["https://instagram.com", "#E1306C", False, "#E1306C", "#4458A7", 0, None],
|
| 72 |
-
["https://twitter.com", "#1DA1F2", False, "#1DA1F2", "#124964", 5, None],
|
| 73 |
-
["https://huggingface.co", "#EE5A55", False, "#EE5A55", "#FBB040", 10, None]
|
| 74 |
-
]
|
| 75 |
-
|
| 76 |
-
iface = gr.Interface(fn=generate_qr_code_app, inputs=inputs, outputs=output, examples=examples)
|
| 77 |
-
iface.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import qrcode
|
| 3 |
|
| 4 |
+
def generate_qrcode(url, color, gradient, gradient_colors, border_radius, logo):
|
| 5 |
+
# Create QR code instance
|
| 6 |
qr = qrcode.QRCode(
|
| 7 |
version=1,
|
| 8 |
+
error_correction=qrcode.constants.ERROR_CORRECT_H,
|
| 9 |
box_size=10,
|
| 10 |
+
border=4,
|
| 11 |
)
|
| 12 |
|
| 13 |
+
# Add data to QR code
|
| 14 |
qr.add_data(url)
|
| 15 |
qr.make(fit=True)
|
| 16 |
|
| 17 |
+
# Create image from QR code
|
| 18 |
+
qr_image = qr.make_image(fill_color=color, back_color="white")
|
| 19 |
+
|
| 20 |
+
# Apply gradient to QR code if enabled
|
| 21 |
+
if gradient:
|
| 22 |
+
qr_image = qr_image.convert("RGB")
|
| 23 |
+
width, height = qr_image.size
|
| 24 |
+
gradient_color1, gradient_color2 = gradient_colors
|
| 25 |
+
for i in range(height):
|
| 26 |
+
shift = int((i / height) * 255)
|
| 27 |
+
fill_color = tuple(int(gradient_color1[c] + shift * (gradient_color2[c] - gradient_color1[c]) / 255) for c in range(3))
|
| 28 |
+
for j in range(width):
|
| 29 |
+
pixel = qr_image.getpixel((j, i))
|
| 30 |
+
if pixel == (0, 0, 0):
|
| 31 |
+
qr_image.putpixel((j, i), fill_color)
|
| 32 |
+
|
| 33 |
+
# Apply border radius to QR code if specified
|
| 34 |
if border_radius > 0:
|
| 35 |
+
qr_image = qr_image.resize((qr_image.size[0] + border_radius*2, qr_image.size[1] + border_radius*2))
|
| 36 |
+
qr_image = qr_image.convert("RGBA")
|
| 37 |
+
width, height = qr_image.size
|
| 38 |
+
border_radius = max(1, min(border_radius, int(width/2), int(height/2)))
|
| 39 |
+
mask = Image.new('L', (width, height), 0)
|
| 40 |
+
draw = ImageDraw.Draw(mask)
|
| 41 |
+
draw.ellipse((0, 0, 2*border_radius, 2*border_radius), fill=255)
|
| 42 |
+
draw.ellipse((width-2*border_radius, 0, width, 2*border_radius), fill=255)
|
| 43 |
+
draw.ellipse((0, height-2*border_radius, 2*border_radius, height), fill=255)
|
| 44 |
+
draw.ellipse((width-2*border_radius, height-2*border_radius, width, height), fill=255)
|
| 45 |
+
alpha = Image.new('L', (width, height), int(255 * 0.5))
|
| 46 |
+
qr_image.paste(alpha, (0, 0), mask)
|
| 47 |
+
|
| 48 |
+
# Add logo to center of QR code if specified
|
| 49 |
+
if logo is not None:
|
| 50 |
+
logo_image = Image.open(logo).convert("RGBA")
|
| 51 |
+
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)
|
| 52 |
+
|
| 53 |
+
# Save QR code as PNG
|
| 54 |
+
qr_image.save("qr_code.png")
|
| 55 |
+
|
| 56 |
+
return "qr_code.png"
|
| 57 |
+
|
| 58 |
+
iface = gr.Interface(
|
| 59 |
+
fn=generate_qrcode,
|
| 60 |
+
inputs=["text", "text", "checkbox", "text[]", "number", gr.inputs.File()],
|
| 61 |
+
outputs="image",
|
| 62 |
+
title="QR Code Generator",
|
| 63 |
+
description="Enter a URL and customize the QR code appearance",
|
| 64 |
+
examples=[["instagram.com", "#FF0000", True, ["#FF0000", "#0000FF"], 10, None],
|
| 65 |
+
["twitter.com", "#00FF00", False, [], 0, "logo.png"],
|
| 66 |
+
["huggingface.co", "#0000FF", True, ["#FF0000", "#00FF00"], 15, "logo.png"]]
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|