File size: 2,999 Bytes
13185ad
 
 
 
 
 
 
 
 
 
 
5df5184
 
13185ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5df5184
13185ad
 
 
 
 
 
 
 
5df5184
13185ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8a48dc
13185ad
 
c61d690
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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()