-
Notifications
You must be signed in to change notification settings - Fork 0
/
genqr.py
69 lines (57 loc) · 2.37 KB
/
genqr.py
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
"""
A Streamlit app for generating colorful QR codes.
This script uses the Streamlit framework to create a web interface for
generating QR codes with custom data, color, version, border, and resolution.
The generated QR code is displayed as an image on the web page.
Imports:
st (streamlit): The Streamlit library for creating web apps.
genqr (function): A custom function from `qr.py` to generate QR codes.
Image (PIL.Image): The Python Imaging Library (PIL) for handling images.
BytesIO (io.BytesIO): A module for handling byte streams.
"""
import base64
import streamlit as st
from qr import genqr
from PIL import Image
from io import BytesIO
# Load favicon
st.set_page_config(
page_title="GenQR",
page_icon="🌌",
)
def get_image_download_link(img, filename):
buffered = BytesIO()
img.save(buffered, format="JPEG")
return buffered.getvalue()
st.title(':violet[Gen]:rainbow[QR] 🦄')
image_placeholder = st.empty()
download_button_placeholder = st.empty()
with st.form("custom-qr"):
st.write("Generate Colourful QR Codes")
data = st.text_input("Enter Some Data")
color = st.color_picker("Choose Color", "#FF4B4B")
version = st.slider("Choose QR Version", min_value=1,
max_value=25, value=5)
border = st.slider("Choose Border", min_value=0, max_value=5, value=3)
size = st.slider("Choose Resolution", min_value=10,
max_value=100, step=10, value=100)
# Every form must have a submit button.
submitted = st.form_submit_button("Submit")
if submitted:
if not data:
st.error("Please enter some data for the QR code.")
else:
qr = genqr(data, color, size, border, version)
if isinstance(qr, str) and "Something went wrong" in qr:
st.error(qr)
else:
result = Image.open(qr)
image_placeholder.image(result)
# Provide a download button outside the form
image_bytes = get_image_download_link(result, "generated_qr.jpg")
download_button_placeholder.download_button(
label="Download QR Code",
data=image_bytes,
file_name="generated_qr.jpg",
mime="image/jpeg"
)