nikzarifie commited on
Commit
13185ad
1 Parent(s): 562bbdd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -0
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import qrcode
2
+ import gradio as gr
3
+ from io import BytesIO
4
+ from PIL import Image, ImageDraw, ImageFont
5
+
6
+ def generate_vcard(name, contact_number_work, contact_number_mobile, email, designation, website, work_address):
7
+ vcard = f"""
8
+ BEGIN:VCARD
9
+ VERSION:3.0
10
+ FN:{name}
11
+ TEL;TYPE=WORK,VOICE:{contact_number_work}
12
+ TEL;TYPE=MOBILE,VOICE:{contact_number_mobile}
13
+ EMAIL;TYPE=PREF,INTERNET:{email}
14
+ TITLE:{designation}
15
+ URL:{website}
16
+ ADR;TYPE=WORK:;;{work_address}
17
+ END:VCARD
18
+ """
19
+
20
+ qr = qrcode.QRCode(
21
+ version=1,
22
+ error_correction=qrcode.constants.ERROR_CORRECT_L,
23
+ box_size=10,
24
+ border=4,
25
+ )
26
+ qr.add_data(vcard)
27
+ qr.make(fit=True)
28
+
29
+ img = qr.make_image(fill_color="black", back_color="white")
30
+
31
+ buffer = BytesIO()
32
+ img.save(buffer, format="PNG")
33
+ buffer.seek(0)
34
+
35
+ return Image.open(buffer)
36
+
37
+ def create_business_card(name, contact_number_work, contact_number_mobile, email, designation, website, work_address, user_image):
38
+ qr_code_img = generate_vcard(name, contact_number_work, contact_number_mobile, email, designation, website, work_address)
39
+
40
+ card_width, card_height = 600, 300
41
+ card = Image.new('RGB', (card_width, card_height), 'white')
42
+ draw = ImageDraw.Draw(card)
43
+
44
+ try:
45
+ # Load and resize the user image
46
+ user_image = Image.open(user_image).convert("RGBA")
47
+ user_image = user_image.resize((150, 150), Image.ANTIALIAS)
48
+ card.paste(user_image, (20, 75), user_image)
49
+ except Exception as e:
50
+ print(f"Error loading image: {e}")
51
+
52
+ # Paste QR code on the card
53
+ qr_code_img = qr_code_img.resize((100, 100), Image.ANTIALIAS)
54
+ card.paste(qr_code_img, (480, 20))
55
+
56
+ # Add text to the business card
57
+ draw.text((190, 50), f"Name: {name}", fill="black")
58
+ draw.text((190, 70), f"Work Contact Number: {contact_number_work}", fill="black")
59
+ draw.text((190, 90), f"Mobile Contact Number: {contact_number_mobile}", fill="black")
60
+ draw.text((190, 110), f"Work Email: {email}", fill="black")
61
+ draw.text((190, 130), f"Designation: {designation}", fill="black")
62
+ draw.text((190, 150), f"Work Website: {website}", fill="black")
63
+ draw.text((190, 170), f"Work Address: {work_address}", fill="black")
64
+
65
+ buffer = BytesIO()
66
+ card.save(buffer, format="PNG")
67
+ buffer.seek(0)
68
+
69
+ return Image.open(buffer)
70
+
71
+ iface = gr.Interface(
72
+ fn=create_business_card,
73
+ inputs=[
74
+ gr.Textbox(label="Name"),
75
+ gr.Textbox(label="Work Contact Number"),
76
+ gr.Textbox(label="Mobile Contact Number"),
77
+ gr.Textbox(label="Work Email"),
78
+ gr.Textbox(label="Designation"),
79
+ gr.Textbox(label="Work Website"),
80
+ gr.Textbox(label="Work Address"),
81
+ gr.Image(type="filepath", label="Upload Image (optional)")
82
+ ],
83
+ outputs=gr.Image(type="pil"),
84
+ title="NMZH-QR-Code & Business Card Generator",
85
+ description="Fill in your details, upload an image, and generate a business card with a QR code containing your information."
86
+ )
87
+
88
+ iface.launch()