Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,84 +3,65 @@ from PIL import Image
|
|
| 3 |
import base64
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
-
# Function to
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
| 11 |
-
# Construct the final URL to embed into the QR code
|
| 12 |
-
final_url = f"https://flowly-ai.vercel.app/tools/qr/pro-qr-codes/read/{encoded_text}"
|
| 13 |
-
|
| 14 |
-
# Create the QR code
|
| 15 |
qr = qrcode.QRCode(
|
| 16 |
version=1,
|
| 17 |
-
error_correction=
|
| 18 |
-
box_size=
|
| 19 |
-
border=
|
| 20 |
)
|
| 21 |
qr.add_data(final_url)
|
| 22 |
qr.make(fit=True)
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
# Resize logo to fit in the QR code
|
| 35 |
-
logo = logo.resize((qr_width // 5, qr_height // 5)) # Resize to be 20% of the QR code size
|
| 36 |
-
|
| 37 |
-
# Calculate position to place logo at the center
|
| 38 |
-
logo_position = ((qr_width - logo_width) // 2, (qr_height - logo_height) // 2)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
img_qr.paste(logo, logo_position, logo)
|
| 42 |
-
|
| 43 |
-
# Save the QR code image
|
| 44 |
-
output_path = "qr_code_with_logo.png"
|
| 45 |
img_qr.save(output_path)
|
| 46 |
-
|
| 47 |
return output_path
|
| 48 |
|
| 49 |
-
# Function to
|
| 50 |
-
def
|
| 51 |
-
|
| 52 |
-
with open(file, 'r') as f:
|
| 53 |
-
text = f.read()
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
def qr_code_file_interface(file: str):
|
| 64 |
-
result = file2qrcode(file)
|
| 65 |
-
return result
|
| 66 |
|
| 67 |
-
#
|
| 68 |
interface = gr.Interface(
|
| 69 |
fn=qr_code_interface,
|
| 70 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
outputs=gr.Image(type="filepath", label="Generated QR Code"),
|
| 72 |
-
description="Generate a QR Code from
|
| 73 |
-
css="footer {visibility: hidden}"
|
| 74 |
-
)
|
| 75 |
-
|
| 76 |
-
# Create a second Gradio interface for file input
|
| 77 |
-
file_interface = gr.Interface(
|
| 78 |
-
fn=qr_code_file_interface,
|
| 79 |
-
inputs=gr.File(label="Upload Text File"),
|
| 80 |
-
outputs=gr.Image(type="filepath", label="Generated QR Code from File"),
|
| 81 |
-
description="Upload a text file, and generate a QR code based on the file's content."
|
| 82 |
)
|
| 83 |
|
| 84 |
-
# Launch the
|
| 85 |
interface.launch(share=True)
|
| 86 |
-
file_interface.launch(share=True)
|
|
|
|
| 3 |
import base64
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
# Function to encode text or file content in base64 and generate a QR code
|
| 7 |
+
def generate_qr(data: str, error_correction: int = qrcode.constants.ERROR_CORRECT_L, box_size: int = 10, border: int = 4, fill_color: str = "black", back_color: str = "white", logo_path: str = None):
|
| 8 |
+
encoded_data = base64.b64encode(data.encode('utf-8')).decode('utf-8')
|
| 9 |
+
final_url = f"https://flowly-ai.vercel.app/tools/qr/qr-code/read/{encoded_data}"
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
qr = qrcode.QRCode(
|
| 12 |
version=1,
|
| 13 |
+
error_correction=error_correction,
|
| 14 |
+
box_size=box_size,
|
| 15 |
+
border=border,
|
| 16 |
)
|
| 17 |
qr.add_data(final_url)
|
| 18 |
qr.make(fit=True)
|
| 19 |
+
img_qr = qr.make_image(fill_color=fill_color, back_color=back_color)
|
| 20 |
|
| 21 |
+
if logo_path:
|
| 22 |
+
try:
|
| 23 |
+
logo = Image.open(logo_path).convert("RGBA")
|
| 24 |
+
qr_width, qr_height = img_qr.size
|
| 25 |
+
logo = logo.resize((qr_width // 5, qr_height // 5))
|
| 26 |
+
logo_position = ((qr_width - logo.size[0]) // 2, (qr_height - logo.size[1]) // 2)
|
| 27 |
+
img_qr.paste(logo, logo_position, logo)
|
| 28 |
+
except Exception as e:
|
| 29 |
+
print(f"Error loading logo: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
output_path = "qr_code.png"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
img_qr.save(output_path)
|
|
|
|
| 33 |
return output_path
|
| 34 |
|
| 35 |
+
# Function to handle text or file input
|
| 36 |
+
def qr_code_interface(input_type, text, file, error_correction, box_size, border, fill_color, back_color, logo):
|
| 37 |
+
logo_path = logo.name if logo else None
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
if input_type == "Text" and text:
|
| 40 |
+
return generate_qr(text, error_correction, box_size, border, fill_color, back_color, logo_path)
|
| 41 |
+
elif input_type == "File" and file:
|
| 42 |
+
with open(file.name, 'r') as f:
|
| 43 |
+
file_data = f.read()
|
| 44 |
+
return generate_qr(file_data, error_correction, box_size, border, fill_color, back_color, logo_path)
|
| 45 |
+
else:
|
| 46 |
+
return None
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
# Gradio Interface
|
| 49 |
interface = gr.Interface(
|
| 50 |
fn=qr_code_interface,
|
| 51 |
+
inputs=[
|
| 52 |
+
gr.Radio(["Text", "File"], label="Input Type", value="Text"),
|
| 53 |
+
gr.Textbox(label="Enter Text", visible=True),
|
| 54 |
+
gr.File(label="Upload Text File", type="file", visible=False),
|
| 55 |
+
gr.Slider(0, 3, 1, label="Error Correction", value=0),
|
| 56 |
+
gr.Slider(1, 20, 1, label="Box Size", value=10),
|
| 57 |
+
gr.Slider(1, 10, 1, label="Border Size", value=4),
|
| 58 |
+
gr.ColorPicker(label="QR Fill Color", value="#000000"),
|
| 59 |
+
gr.ColorPicker(label="QR Background Color", value="#FFFFFF"),
|
| 60 |
+
gr.File(label="Upload Logo (Optional)")
|
| 61 |
+
],
|
| 62 |
outputs=gr.Image(type="filepath", label="Generated QR Code"),
|
| 63 |
+
description="Generate a QR Code from either text or file with custom styles, colors, and an optional logo."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
)
|
| 65 |
|
| 66 |
+
# Launch the app
|
| 67 |
interface.launch(share=True)
|
|
|