bonrix commited on
Commit
40722f2
1 Parent(s): a4f2ecb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import qrcode
3
+ from PIL import Image, ImageDraw, ImageFont
4
+ import time
5
+ import os
6
+
7
+ def generate_upi_qr(upi_code, amount, merchant_name=None):
8
+ if not merchant_name:
9
+ merchant_name = upi_code.split('@')[0]
10
+
11
+ # Generate UPI QR code
12
+ qr = qrcode.QRCode(version=1, box_size=10, border=5)
13
+ qr_data = f"upi://pay?pa={upi_code}&pn={merchant_name}&am={amount}&cu=INR"
14
+
15
+ qr.add_data(qr_data)
16
+ qr.make(fit=True)
17
+ qr_image = qr.make_image(fill="black", back_color="white").convert("RGB")
18
+
19
+ # Generate unique image name based on UPI ID, amount, and timestamp
20
+ timestamp = str(int(time.time()))
21
+ image_name = f"qr_{upi_code}_{amount}_{timestamp}.png"
22
+
23
+ # Load your template
24
+ template = Image.open(gradio.interface.assets.get("Asset 6-1.png")).convert("RGB")
25
+
26
+ # Resize the QR code if needed
27
+ qr_image = qr_image.resize((700, 700))
28
+
29
+ # Paste the QR code onto your template
30
+ template.paste(qr_image, (225, 450))
31
+
32
+ # Draw text for amount and merchant name
33
+ draw = ImageDraw.Draw(template)
34
+ font = ImageFont.truetype("arial.ttf", 50) # Specify the font file and size
35
+
36
+ text_amount = f"Amount: {amount} INR"
37
+ text_merchant = f"Merchant: {merchant_name}"
38
+
39
+ w_amount, h_amount = draw.textsize(text_amount, font=font)
40
+ w_merchant, h_merchant = draw.textsize(text_merchant, font=font)
41
+
42
+ width, height = template.size
43
+
44
+ position_amount = ((width - w_amount) // 2, height - 1145) # Adjust the height as needed
45
+ position_merchant = ((width - w_merchant) // 2, height - 1205) # Adjust the height as needed
46
+
47
+ draw.text(position_amount, text_amount, (0, 0, 0), font=font)
48
+ draw.text(position_merchant, text_merchant, (0, 0, 0), font=font)
49
+
50
+ # Save the result
51
+ final_image_path = os.path.join('static', 'qr_images', f'final_{image_name}')
52
+ template.save(final_image_path)
53
+
54
+ return final_image_path
55
+
56
+ def generate_upi_qr_interface(upi_code, amount, merchant_name=None):
57
+ image_path = generate_upi_qr(upi_code, amount, merchant_name)
58
+ return image_path
59
+
60
+ iface = gr.Interface(
61
+ fn=generate_upi_qr_interface,
62
+ inputs=["text", "text", "text"],
63
+ outputs="image",
64
+ title="Generate UPI QR Code",
65
+ description="Generate a UPI QR code image."
66
+ )
67
+
68
+ if __name__ == "__main__":
69
+ iface.launch()