PRO-QR-Codes / app.py
Adityadn's picture
Create app.py
af74360 verified
raw
history blame
1.84 kB
import qrcode
from PIL import Image
import gradio as gr
# Function to generate QR code with a logo
def generate_qr_with_logo(data: str, logo_path: str):
# Create the QR code
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H, # High error correction for logo
box_size=10,
border=4,
)
qr.add_data(data)
qr.make(fit=True)
# Create an image for the QR code
img_qr = qr.make_image(fill='black', back_color='white')
# Open the logo image
logo = Image.open(logo_path) # Provide logo path
logo_size = 50 # Adjust size of the logo
logo = logo.resize((logo_size, logo_size))
# Get QR code dimensions
qr_width, qr_height = img_qr.size
# Calculate logo position to center it
logo_x = (qr_width - logo_size) // 2
logo_y = (qr_height - logo_size) // 2
# Paste the logo onto the QR code
img_qr.paste(logo, (logo_x, logo_y), logo)
# Save and return the final QR code with logo
output_path = "qr_with_logo.png"
img_qr.save(output_path)
return output_path
# Gradio Interface
def qr_code_interface(data: str, logo_path: str):
result = generate_qr_with_logo(data, logo_path)
return result
# Create a Gradio interface
interface = gr.Interface(
fn=qr_code_interface,
inputs=[
gr.Textbox(label="Enter Data for QR Code", placeholder="Enter URL or text here..."),
gr.File(label="Upload Logo", type="file") # File input for the logo
],
outputs=gr.Image(type="file", label="Generated QR Code with Logo"),
description="Generate a QR Code with your custom logo placed in the center. You can provide any text or URL as data and upload an image to be used as the logo.",
css="footer {visibility: hidden}"
)
# Launch the Gradio app
interface.launch()