bonrix commited on
Commit
41d5d66
1 Parent(s): 5d170cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -31
app.py CHANGED
@@ -4,7 +4,7 @@ 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
 
@@ -16,50 +16,45 @@ def generate_upi_qr(upi_code, amount, merchant_name=None):
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 qr_image
55
 
 
56
 
57
  iface = gr.Interface(
58
- fn=generate_upi_qr,
59
  inputs=["text", "text", "text"],
60
  outputs="image",
61
- title="Generate UPI QR Code",
62
- description="Generate a UPI QR code image."
63
  )
64
 
65
  if __name__ == "__main__":
 
4
  import time
5
  import os
6
 
7
+ def generate_upi_qr_sticker(upi_code, amount, merchant_name=None):
8
  if not merchant_name:
9
  merchant_name = upi_code.split('@')[0]
10
 
 
16
  qr.make(fit=True)
17
  qr_image = qr.make_image(fill="black", back_color="white").convert("RGB")
18
 
19
+ # Open your template
20
+ template = Image.open('temp.png').convert("RGB")
 
21
 
22
+ # Resize the QR code if needed
23
+ qr_image = qr_image.resize((700, 700))
24
 
25
+ # Paste the QR code onto your template
26
+ template.paste(qr_image, (225, 450))
27
 
28
+ # Draw text for amount and merchant name
29
+ draw = ImageDraw.Draw(template)
30
+ font = ImageFont.truetype("arial.ttf", 50) # Specify the font file and size
31
 
32
+ text_amount = f"Amount: {amount} INR"
33
+ text_merchant = f"Merchant: {merchant_name}"
 
34
 
35
+ w_amount, h_amount = draw.textsize(text_amount, font=font)
36
+ w_merchant, h_merchant = draw.textsize(text_merchant, font=font)
37
 
38
+ width, height = template.size
 
39
 
40
+ position_amount = ((width - w_amount) // 2, height - 1145) # Adjust the height as needed
41
+ position_merchant = ((width - w_merchant) // 2, height - 1205) # Adjust the height as needed
42
 
43
+ draw.text(position_amount, text_amount, (0, 0, 0), font=font)
44
+ draw.text(position_merchant, text_merchant, (0, 0, 0), font=font)
45
 
46
+ # Save the result
47
+ final_image_path = os.path.join('static', 'qr_images', f'final_{upi_code}.png')
48
+ template.save(final_image_path)
 
 
 
 
 
49
 
50
+ return final_image_path
51
 
52
  iface = gr.Interface(
53
+ fn=generate_upi_qr_sticker,
54
  inputs=["text", "text", "text"],
55
  outputs="image",
56
+ title="Generate UPI QR Code Sticker",
57
+ description="Generate a UPI QR code sticker image."
58
  )
59
 
60
  if __name__ == "__main__":