-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
123 lines (103 loc) · 4.62 KB
/
Copy pathapp.py
File metadata and controls
123 lines (103 loc) · 4.62 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import os
import qrcode
from PIL import Image, ImageDraw, ImageFont, ImageOps
import streamlit as st
import io
st.set_page_config(page_title="QR Code Generator", layout="centered")
# 1. Custom CSS Injector to create the Application Border Frame Layout
st.markdown("""
<style>
/* Wraps the entire content block inside a structured business card frame */
.block-container {
border: 2px solid #E0E4EC;
border-radius: 12px;
padding: 40px !important;
background-color: #FFFFFF;
box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.03);
margin-top: 30px;
margin-bottom: 30px;
}
/* Adds a clean layout border block styling around the main container */
.main .block-container {
max-width: 700px !important;
}
</style>
""", unsafe_allow_html=True)
# 2. Application Header
st.title("📊 QR Generator")
st.write("Generate high-resolution, branded QR codes.")
# 3. Streamlit Interactive Inputs with Guide Instructions Placed Below
target_url = st.text_input("👉 ENTER URL:")
st.caption("💡 *Example to enter: https://github.com or your live deployed app link*")
display_name = st.text_input("👤 ENTER YOUR NAME:")
st.caption("💡 *Example to enter: Appala Srinivas Tanakala*")
display_title = st.text_input("💼 ENTER YOUR TITLE:")
st.caption("💡 *Example to enter: Data Scientist & AI / Fintech Leader*")
# 4. Linux System Font Routing Fix
font_path = None
possible_paths = [
"/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf",
"/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
"/usr/share/fonts/truetype/ubuntu/Ubuntu-B.ttf"
]
for path in possible_paths:
if os.path.exists(path):
font_path = path
break
# 5. Action button validation sequence
if st.button("Generate QR Code") or target_url:
if target_url.strip():
# Configure high-resolution QR matrix
qr = qrcode.QRCode(
version=4,
error_correction=qrcode.constants.ERROR_CORRECT_M,
box_size=14,
border=4,
)
qr.add_data(target_url.strip())
qr.make(fit=True)
# Convert to RGB color canvas
qr_img = qr.make_image(fill_color="black", back_color="white").convert('RGB')
qr_width, qr_height = qr_img.size
# Set up dynamic white canvas buffer space for clear text spacing
extra_bottom_space = 110
new_width = qr_width
new_height = qr_height + extra_bottom_space
final_img = Image.new("RGB", (new_width, new_height), "white")
final_img.paste(qr_img, (0, 0))
draw = ImageDraw.Draw(final_img)
# Initialize the explicit sharp fonts with clean system fallback structures
if font_path:
name_font = ImageFont.truetype(font_path, 26) # Bold and readable
title_font = ImageFont.truetype(font_path, 18) # Clean secondary text
else:
name_font = title_font = ImageFont.load_default()
# Helper function to auto-center text layers accurately
def draw_centered_text(text_to_print, y_position, font_style, text_color="black"):
bbox = draw.textbbox((0, 0), text_to_print, font=font_style)
text_w = bbox[2] - bbox[0]
x_position = (new_width - text_w) // 2
draw.text((x_position, y_position), text_to_print, fill=text_color, font=font_style)
# Print typography fields on bottom white canvas
print_name = display_name.strip() if display_name.strip() else "Appala Srinivas Tanakala"
print_title = display_title.strip() if display_title.strip() else "Data Scientist"
draw_centered_text(print_name, qr_height + 15, name_font, text_color="black")
draw_centered_text(print_title, qr_height + 55, title_font, text_color="#555555")
# Add an inner frame border around the image asset file itself
final_img = ImageOps.expand(final_img, border=3, fill="#333333")
# 6. Display the image inside your new app frame layout
st.write("---")
st.image(final_img, caption="Preview of your framed QR badge", use_container_width=False, width=350)
# Convert the framed PIL image array into downloadable bytes stream
img_buffer = io.BytesIO()
final_img.save(img_buffer, format="PNG")
byte_data = img_buffer.getvalue()
# Download button forcing filename explicitly to generated_qr
st.download_button(
label="⬇️ Download Framed QR Code",
data=byte_data,
file_name="generated_qr.png",
mime="image/png"
)
else:
st.error("❌ Error: The URL input field cannot be left blank.")