File size: 1,837 Bytes
af74360
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
59
60
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()