Adityadn commited on
Commit
af74360
·
verified ·
1 Parent(s): 8010030

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import qrcode
2
+ from PIL import Image
3
+ import gradio as gr
4
+
5
+ # Function to generate QR code with a logo
6
+ def generate_qr_with_logo(data: str, logo_path: str):
7
+ # Create the QR code
8
+ qr = qrcode.QRCode(
9
+ version=1,
10
+ error_correction=qrcode.constants.ERROR_CORRECT_H, # High error correction for logo
11
+ box_size=10,
12
+ border=4,
13
+ )
14
+ qr.add_data(data)
15
+ qr.make(fit=True)
16
+
17
+ # Create an image for the QR code
18
+ img_qr = qr.make_image(fill='black', back_color='white')
19
+
20
+ # Open the logo image
21
+ logo = Image.open(logo_path) # Provide logo path
22
+ logo_size = 50 # Adjust size of the logo
23
+ logo = logo.resize((logo_size, logo_size))
24
+
25
+ # Get QR code dimensions
26
+ qr_width, qr_height = img_qr.size
27
+
28
+ # Calculate logo position to center it
29
+ logo_x = (qr_width - logo_size) // 2
30
+ logo_y = (qr_height - logo_size) // 2
31
+
32
+ # Paste the logo onto the QR code
33
+ img_qr.paste(logo, (logo_x, logo_y), logo)
34
+
35
+ # Save and return the final QR code with logo
36
+ output_path = "qr_with_logo.png"
37
+ img_qr.save(output_path)
38
+
39
+ return output_path
40
+
41
+ # Gradio Interface
42
+ def qr_code_interface(data: str, logo_path: str):
43
+ result = generate_qr_with_logo(data, logo_path)
44
+ return result
45
+
46
+ # Create a Gradio interface
47
+ interface = gr.Interface(
48
+ fn=qr_code_interface,
49
+ inputs=[
50
+ gr.Textbox(label="Enter Data for QR Code", placeholder="Enter URL or text here..."),
51
+ gr.File(label="Upload Logo", type="file") # File input for the logo
52
+ ],
53
+ outputs=gr.Image(type="file", label="Generated QR Code with Logo"),
54
+ 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.",
55
+ css="footer {visibility: hidden}"
56
+ )
57
+
58
+ # Launch the Gradio app
59
+ interface.launch()