nikzarifie's picture
Update app.py
0f551d0 verified
raw
history blame contribute delete
No virus
3 kB
import qrcode
import gradio as gr
from io import BytesIO
from PIL import Image, ImageDraw, ImageFont
def generate_vcard(name, contact_number_work, contact_number_mobile, email, designation, website, work_address):
vcard = f"""
BEGIN:VCARD
VERSION:3.0
FN:{name}
TEL;TYPE=WORK,VOICE:{contact_number_work}
TEL;TYPE=MOBILE,VOICE:{contact_number_mobile}
EMAIL;TYPE=PREF,INTERNET:{email}
TITLE:{designation}
URL:{website}
ADR;TYPE=WORK:;;{work_address}
END:VCARD
"""
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(vcard)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
buffer = BytesIO()
img.save(buffer, format="PNG")
buffer.seek(0)
return Image.open(buffer)
def create_business_card(name, contact_number_work, contact_number_mobile, email, designation, website, work_address, user_image):
qr_code_img = generate_vcard(name, contact_number_work, contact_number_mobile, email, designation, website, work_address)
card_width, card_height = 600, 300
card = Image.new('RGB', (card_width, card_height), 'white')
draw = ImageDraw.Draw(card)
try:
# Load and resize the user image
user_image = Image.open(user_image).convert("RGBA")
user_image = user_image.resize((150, 150), Image.ANTIALIAS)
card.paste(user_image, (20, 75), user_image)
except Exception as e:
print(f"Error loading image: {e}")
# Paste QR code on the card
qr_code_img = qr_code_img.resize((100, 100), Image.ANTIALIAS)
card.paste(qr_code_img, (480, 20))
# Add text to the business card
draw.text((190, 50), f"Name: {name}", fill="black")
draw.text((190, 70), f"Work Contact Number: {contact_number_work}", fill="black")
draw.text((190, 90), f"Mobile Contact Number: {contact_number_mobile}", fill="black")
draw.text((190, 110), f"Work Email: {email}", fill="black")
draw.text((190, 130), f"Designation: {designation}", fill="black")
draw.text((190, 150), f"Work Website: {website}", fill="black")
draw.text((190, 170), f"Work Address: {work_address}", fill="black")
buffer = BytesIO()
card.save(buffer, format="PNG")
buffer.seek(0)
return Image.open(buffer)
iface = gr.Interface(
fn=create_business_card,
inputs=[
gr.Textbox(label="Name"),
gr.Textbox(label="Work Contact Number"),
gr.Textbox(label="Mobile Contact Number"),
gr.Textbox(label="Work Email"),
gr.Textbox(label="Designation"),
gr.Textbox(label="Work Website"),
gr.Textbox(label="Work Address"),
gr.Image(type="filepath", label="Upload Image (optional)")
],
outputs=gr.Image(type="pil"),
title="NMZH-QR-Code & Business Card Generator",
description="Fill in your details, upload an image, and generate a business card with a QR code containing your information."
)
iface.launch()