Spaces:
Build error
Build error
Commit ·
109506a
1
Parent(s): 4de2d7d
Added code to generate simple qr, qr with logo and animated qr codes
Browse files- README.md +5 -0
- app.py +181 -2
- assets/animations/contact.json +0 -0
- assets/animations/scanner.json +0 -0
- assets/qrcode.png +0 -0
- utils.py +66 -0
README.md
CHANGED
|
@@ -10,3 +10,8 @@ pinned: false
|
|
| 10 |
---
|
| 11 |
|
| 12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 13 |
+
|
| 14 |
+
### Resources
|
| 15 |
+
- https://realpython.com/python-generate-qr-code/
|
| 16 |
+
- https://github.com/realpython/materials/tree/master/python-qr-code/
|
| 17 |
+
- https://segno.readthedocs.io/en/latest/artistic-qrcodes.html
|
app.py
CHANGED
|
@@ -1,4 +1,183 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 1 |
+
import io
|
| 2 |
+
from urllib.request import urlopen
|
| 3 |
+
|
| 4 |
+
import qrcode
|
| 5 |
+
import segno
|
| 6 |
import streamlit as st
|
| 7 |
+
from PIL import Image
|
| 8 |
+
from qrcode.image.styledpil import StyledPilImage
|
| 9 |
+
from qrcode.image.styles.colormasks import HorizontalGradiantColorMask
|
| 10 |
+
from streamlit_lottie import st_lottie
|
| 11 |
+
|
| 12 |
+
from utils import hide_footer, lottie_local
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def configure_page():
|
| 16 |
+
"""
|
| 17 |
+
Configures the Streamlit page settings.
|
| 18 |
+
"""
|
| 19 |
+
st.set_page_config(
|
| 20 |
+
page_icon="🤳",
|
| 21 |
+
page_title="QR Code Generator",
|
| 22 |
+
layout="centered",
|
| 23 |
+
initial_sidebar_state="expanded",
|
| 24 |
+
menu_items={
|
| 25 |
+
"About": "## A minimalistic application to generate QR Codes using Python"
|
| 26 |
+
},
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def display_title_and_animation():
|
| 31 |
+
"""
|
| 32 |
+
Displays the title and the Lottie animation.
|
| 33 |
+
"""
|
| 34 |
+
st.title("QR Code Generator")
|
| 35 |
+
hide_footer()
|
| 36 |
+
anim = lottie_local("assets/animations/scanner.json")
|
| 37 |
+
st_lottie(
|
| 38 |
+
anim, speed=2, reverse=False, loop=True, quality="medium", height=300, width=300
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def get_user_input():
|
| 43 |
+
"""
|
| 44 |
+
Gets the URL input from the user.
|
| 45 |
+
"""
|
| 46 |
+
return st.text_input(label="Enter the URL to convert into a QR code")
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def get_gif_input():
|
| 50 |
+
"""
|
| 51 |
+
Gets the gif URL input from the user which should be used as a background.
|
| 52 |
+
"""
|
| 53 |
+
return st.text_input(label="Enter gif URL which should be used in a background")
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def get_qr_code_parameters():
|
| 57 |
+
"""
|
| 58 |
+
Gets the QR code parameters from the user.
|
| 59 |
+
"""
|
| 60 |
+
col1, col2 = st.columns(2)
|
| 61 |
+
with col1:
|
| 62 |
+
fill_color = st.color_picker("Pick Fill Color", "#000000")
|
| 63 |
+
with col2:
|
| 64 |
+
back_color = st.color_picker("Pick Background Color", "#ffffff")
|
| 65 |
+
return fill_color, back_color
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def generate_animated_qr_code(data, gif_file):
|
| 69 |
+
img_path = "animated_qrcode.gif"
|
| 70 |
+
slts_qrcode = segno.make_qr(data)
|
| 71 |
+
# gif = urlopen(gif_url)
|
| 72 |
+
slts_qrcode.to_artistic(
|
| 73 |
+
background=gif_file,
|
| 74 |
+
target=img_path,
|
| 75 |
+
light="white",
|
| 76 |
+
scale=10,
|
| 77 |
+
)
|
| 78 |
+
return img_path
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def generate_simple_qr_code(data, fill_color, back_color):
|
| 82 |
+
"""
|
| 83 |
+
Generates and saves the QR code based on the user input.
|
| 84 |
+
"""
|
| 85 |
+
qr = qrcode.QRCode(version=4, box_size=10, border=4)
|
| 86 |
+
qr.add_data(data)
|
| 87 |
+
qr.make(fit=True)
|
| 88 |
+
img = qr.make_image(fill_color=fill_color, back_color=back_color)
|
| 89 |
+
img_path = "./assets/qrcode.png"
|
| 90 |
+
img.save(img_path)
|
| 91 |
+
return img_path
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def generate_logo_qr_code(data, logo_file_path):
|
| 95 |
+
out = io.BytesIO()
|
| 96 |
+
# Nothing special here, let Segno generate the QR code and save it as PNG in a buffer
|
| 97 |
+
segno.make(data, error="h").save(out, scale=5, kind="png")
|
| 98 |
+
out.seek(0) # Important to let Pillow load the PNG
|
| 99 |
+
img = Image.open(out)
|
| 100 |
+
img = img.convert("RGB") # Ensure colors for the output
|
| 101 |
+
img_width, img_height = img.size
|
| 102 |
+
logo_max_size = img_height // 3 # May use a fixed value as well
|
| 103 |
+
logo_img = Image.open(logo_file_path) # The logo
|
| 104 |
+
# Resize the logo to logo_max_size
|
| 105 |
+
logo_img.thumbnail((logo_max_size, logo_max_size), Image.Resampling.LANCZOS)
|
| 106 |
+
# Calculate the center of the QR code
|
| 107 |
+
box = ((img_width - logo_img.size[0]) // 2, (img_height - logo_img.size[1]) // 2)
|
| 108 |
+
img.paste(logo_img, box)
|
| 109 |
+
img_path = "qrcode_with_logo.png"
|
| 110 |
+
img.save(img_path)
|
| 111 |
+
return img_path
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def display_qr_code(img_path):
|
| 115 |
+
"""
|
| 116 |
+
Displays the generated QR code and provides a download button.
|
| 117 |
+
"""
|
| 118 |
+
with open(img_path, "rb") as file:
|
| 119 |
+
image = Image.open(file)
|
| 120 |
+
st.image(image, caption="QR Code")
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def display_download_button(
|
| 124 |
+
file_path, filename="your_qr_code.png", mime_type="image/png"
|
| 125 |
+
):
|
| 126 |
+
with open(file_path, "rb") as file:
|
| 127 |
+
st.download_button(
|
| 128 |
+
label="Download Image", data=file, file_name=filename, mime=mime_type
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
def main():
|
| 133 |
+
"""
|
| 134 |
+
Main function for the Streamlit app.
|
| 135 |
+
"""
|
| 136 |
+
configure_page()
|
| 137 |
+
display_title_and_animation()
|
| 138 |
+
url = get_user_input()
|
| 139 |
+
# gif_url = get_gif_input()
|
| 140 |
+
|
| 141 |
+
# Dropdown menu
|
| 142 |
+
options = ["Simple QR code", "QR code with logo", "Animated QR code"]
|
| 143 |
+
selected_option = st.selectbox("Select the type of QR code to generate", options)
|
| 144 |
+
|
| 145 |
+
is_simple_qr = selected_option == "Simple QR code"
|
| 146 |
+
is_logo_qr = selected_option == "QR code with logo"
|
| 147 |
+
is_animated_qr = selected_option == "Animated QR code"
|
| 148 |
+
|
| 149 |
+
if is_simple_qr:
|
| 150 |
+
fill_color, back_color = get_qr_code_parameters()
|
| 151 |
+
elif is_logo_qr:
|
| 152 |
+
logo = st.file_uploader(
|
| 153 |
+
label="Upload your logo",
|
| 154 |
+
accept_multiple_files=False,
|
| 155 |
+
type=["png", "jpg", "jpeg"],
|
| 156 |
+
)
|
| 157 |
+
elif is_animated_qr:
|
| 158 |
+
gif_file = st.file_uploader("Choose a GIF file", type="gif")
|
| 159 |
+
# Read the GIF file into memory
|
| 160 |
+
if gif_file:
|
| 161 |
+
# Display the uploaded GIF
|
| 162 |
+
st.image(gif_file, caption="Uploaded GIF", use_column_width=True)
|
| 163 |
+
|
| 164 |
+
if st.button("Generate") and url:
|
| 165 |
+
if is_simple_qr:
|
| 166 |
+
img_path = generate_simple_qr_code(url, fill_color, back_color)
|
| 167 |
+
image_name = "your_qr_code.png"
|
| 168 |
+
mime_type = "image/png"
|
| 169 |
+
elif is_logo_qr:
|
| 170 |
+
img_path = generate_logo_qr_code(url, logo)
|
| 171 |
+
image_name = "your_qr_code.png"
|
| 172 |
+
mime_type = "image/png"
|
| 173 |
+
elif is_animated_qr:
|
| 174 |
+
img_path = generate_animated_qr_code(url, gif_file)
|
| 175 |
+
image_name = "your_qr_code.gif"
|
| 176 |
+
mime_type = "image/gif"
|
| 177 |
+
|
| 178 |
+
display_qr_code(img_path)
|
| 179 |
+
display_download_button(img_path, image_name, mime_type)
|
| 180 |
+
|
| 181 |
|
| 182 |
+
if __name__ == "__main__":
|
| 183 |
+
main()
|
assets/animations/contact.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
assets/animations/scanner.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
assets/qrcode.png
ADDED
|
utils.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from streamlit_lottie import st_lottie
|
| 3 |
+
import json
|
| 4 |
+
import requests
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def css_local(filepath: str):
|
| 8 |
+
"""
|
| 9 |
+
Method to load the desired stylesheet from the given filepath
|
| 10 |
+
"""
|
| 11 |
+
with open(filepath) as f:
|
| 12 |
+
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def lottie_local(filepath: str):
|
| 16 |
+
"""
|
| 17 |
+
Method to load the desired Lottie Animation from the given filepath
|
| 18 |
+
"""
|
| 19 |
+
with open(filepath, "r") as f:
|
| 20 |
+
return json.load(f)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def lottie_url(url: str):
|
| 24 |
+
"""
|
| 25 |
+
Method to load the desired Lottie Animation from given url
|
| 26 |
+
"""
|
| 27 |
+
r = requests.get(url)
|
| 28 |
+
if r.status_code != 200:
|
| 29 |
+
return None
|
| 30 |
+
return r.json()
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def display_map(l1: list = [22.572645], l2: list = [88.363892], z: int = 9) -> None:
|
| 34 |
+
"""
|
| 35 |
+
Method to display the desired coordinates in a map by using OpenStreetAPI
|
| 36 |
+
|
| 37 |
+
Parameters
|
| 38 |
+
-----------
|
| 39 |
+
l1 : list
|
| 40 |
+
desired latitude coordinate(s); default set for Kolkata ([22.572645])
|
| 41 |
+
l2 : list
|
| 42 |
+
desired longitude coordinate(s); default set for Kolkata ([88.363892])
|
| 43 |
+
z : int
|
| 44 |
+
desired zoom level; default set to metropolitan area level(9)
|
| 45 |
+
|
| 46 |
+
Returns
|
| 47 |
+
--------
|
| 48 |
+
None
|
| 49 |
+
|
| 50 |
+
See Also
|
| 51 |
+
--------
|
| 52 |
+
For plotting multiple cities, simply pass their respective latitude and longitude coordinates in
|
| 53 |
+
the same list
|
| 54 |
+
"""
|
| 55 |
+
map_data = pd.DataFrame(
|
| 56 |
+
{"latitude": np.array(l1), "longitude": np.array(l2)})
|
| 57 |
+
st.map(map_data, zoom=z)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def hide_footer():
|
| 61 |
+
hide_st_style = """
|
| 62 |
+
<style>
|
| 63 |
+
footer {visibility: hidden;}
|
| 64 |
+
</style>
|
| 65 |
+
"""
|
| 66 |
+
st.markdown(hide_st_style, unsafe_allow_html=True)
|