QRapp / app.py
vincentiusyoshuac's picture
Update app.py
57d50d1 verified
import qrcode
from qrcode.constants import ERROR_CORRECT_L
import streamlit as st
import io
from PIL import Image
from urllib.parse import urlencode
def generate_contact_qr(contact_info):
"""
Generates a QR code for contact information in the vCard format.
Parameters:
contact_info (dict): A dictionary containing the contact information fields.
Returns:
PIL.Image.Image: The generated QR code image.
"""
vcard = "BEGIN:VCARD\n"
vcard += f"VERSION:4.0\n"
vcard += f"N:{contact_info['last_name']};{contact_info['first_name']}\n"
vcard += f"EMAIL:{contact_info['email']}\n"
vcard += f"TEL:{contact_info['phone']}\n"
vcard += f"ORG:{contact_info['organization']}\n"
vcard += f"TITLE:{contact_info['job_title']}\n"
vcard += "END:VCARD"
qr = qrcode.QRCode(
version=1,
error_correction=ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(vcard)
qr.make(fit=True)
return qr.make_image(fill_color="black", back_color="white")
def generate_email_qr(email_info):
"""
Generates a QR code for an email.
Parameters:
email_info (dict): A dictionary containing the email information fields.
Returns:
PIL.Image.Image: The generated QR code image.
"""
email_url = f"mailto:{email_info['to']}?subject={email_info['subject']}&body={email_info['body']}"
qr = qrcode.QRCode(
version=1,
error_correction=ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(email_url)
qr.make(fit=True)
return qr.make_image(fill_color="black", back_color="white")
def generate_sms_qr(sms_info):
"""
Generates a QR code for an SMS message.
Parameters:
sms_info (dict): A dictionary containing the SMS information fields.
Returns:
PIL.Image.Image: The generated QR code image.
"""
sms_url = f"sms:{sms_info['to']}?body={sms_info['body']}"
qr = qrcode.QRCode(
version=1,
error_correction=ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(sms_url)
qr.make(fit=True)
return qr.make_image(fill_color="black", back_color="white")
def generate_location_qr(latitude, longitude):
"""
Generates a QR code for a geographic location.
Parameters:
latitude (float): The latitude of the location.
longitude (float): The longitude of the location.
Returns:
PIL.Image.Image: The generated QR code image.
"""
location_url = f"https://www.google.com/maps?q={latitude},{longitude}"
qr = qrcode.QRCode(
version=1,
error_correction=ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(location_url)
qr.make(fit=True)
return qr.make_image(fill_color="black", back_color="white")
def generate_link_qr(url):
"""
Generates a QR code for a URL.
Parameters:
url (str): The URL to be encoded.
Returns:
PIL.Image.Image: The generated QR code image.
"""
qr = qrcode.QRCode(
version=1,
error_correction=ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(url)
qr.make(fit=True)
return qr.make_image(fill_color="black", back_color="white")
def main():
st.set_page_config(page_title="Advanced QR Code Generator")
st.title("Advanced QR Code Generator")
tab = st.sidebar.selectbox("Select QR Code Type", ["Contact", "Email", "SMS", "Location", "Link"])
if tab == "Contact":
st.subheader("Contact QR Code")
contact_info = {
"first_name": st.text_input("First Name"),
"last_name": st.text_input("Last Name"),
"email": st.text_input("Email"),
"phone": st.text_input("Phone"),
"organization": st.text_input("Organization"),
"job_title": st.text_input("Job Title"),
}
if st.button("Generate QR Code"):
qr_code = generate_contact_qr(contact_info)
buf = io.BytesIO()
qr_code.save(buf, format="PNG")
st.image(buf.getvalue(), use_container_width=True)
elif tab == "Email":
st.subheader("Email QR Code")
email_info = {
"to": st.text_input("To"),
"subject": st.text_input("Subject"),
"body": st.text_area("Body"),
}
if st.button("Generate QR Code"):
qr_code = generate_email_qr(email_info)
buf = io.BytesIO()
qr_code.save(buf, format="PNG")
st.image(buf.getvalue(), use_container_width=True)
elif tab == "SMS":
st.subheader("SMS QR Code")
sms_info = {
"to": st.text_input("To"),
"body": st.text_area("Message"),
}
if st.button("Generate QR Code"):
qr_code = generate_sms_qr(sms_info)
buf = io.BytesIO()
qr_code.save(buf, format="PNG")
st.image(buf.getvalue(), use_container_width=True)
elif tab == "Location":
st.subheader("Location QR Code")
latitude = st.number_input("Latitude", min_value=-90.0, max_value=90.0, step=0.000001)
longitude = st.number_input("Longitude", min_value=-180.0, max_value=180.0, step=0.000001)
if st.button("Generate QR Code"):
qr_code = generate_location_qr(latitude, longitude)
buf = io.BytesIO()
qr_code.save(buf, format="PNG")
st.image(buf.getvalue(), use_container_width=True)
elif tab == "Link":
st.subheader("Link QR Code")
url = st.text_input("URL")
if st.button("Generate QR Code"):
qr_code = generate_link_qr(url)
buf = io.BytesIO()
qr_code.save(buf, format="PNG")
st.image(buf.getvalue(), use_container_width=True)
if __name__ == "__main__":
main()