Spaces:
Runtime error
Runtime error
import gradio as gr | |
import qrcode | |
from PIL import Image, ImageDraw, ImageFont | |
import time | |
import os | |
def generate_upi_qr_sticker(upi_code, amount, merchant_name=None): | |
if not merchant_name: | |
merchant_name = upi_code.split('@')[0] | |
# Generate UPI QR code | |
qr = qrcode.QRCode(version=1, box_size=10, border=5) | |
qr_data = f"upi://pay?pa={upi_code}&pn={merchant_name}&am={amount}&cu=INR" | |
qr.add_data(qr_data) | |
qr.make(fit=True) | |
qr_image = qr.make_image(fill="black", back_color="white").convert("RGB") | |
# Open your template | |
template = Image.open('temp.png').convert("RGB") | |
# Resize the QR code if needed | |
qr_image = qr_image.resize((700, 700)) | |
# Paste the QR code onto your template | |
template.paste(qr_image, (225, 450)) | |
# Draw text for amount and merchant name | |
draw = ImageDraw.Draw(template) | |
font = ImageFont.truetype("arial.ttf", 50) # Specify the font file and size | |
text_amount = f"Amount: {amount} INR" | |
text_merchant = f"Merchant: {merchant_name}" | |
w_amount, h_amount = draw.textsize(text_amount, font=font) | |
w_merchant, h_merchant = draw.textsize(text_merchant, font=font) | |
width, height = template.size | |
position_amount = ((width - w_amount) // 2, height - 1145) # Adjust the height as needed | |
position_merchant = ((width - w_merchant) // 2, height - 1205) # Adjust the height as needed | |
draw.text(position_amount, text_amount, (0, 0, 0), font=font) | |
draw.text(position_merchant, text_merchant, (0, 0, 0), font=font) | |
# Save the result | |
final_image_path = os.path.join('static', 'qr_images', f'final_{upi_code}.png') | |
template.save(final_image_path) | |
return final_image_path | |
iface = gr.Interface( | |
fn=generate_upi_qr_sticker, | |
inputs=["text", "text", "text"], | |
outputs="image", | |
title="Generate UPI QR Code Sticker", | |
description="Generate a UPI QR code sticker image." | |
) | |
if __name__ == "__main__": | |
iface.launch() | |