File size: 8,264 Bytes
e0c49d5
11872ae
 
 
 
 
 
 
e0c49d5
11872ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import streamlit as st
import qrcode
from qrcode.image.styledpil import StyledPilImage
from qrcode.image.styles.moduledrawers import RoundedModuleDrawer, CircleModuleDrawer, SquareModuleDrawer
from qrcode.image.styles.colorfills import SolidFillColorMask, RadialGradiantColorMask, SquareGradiantColorMask
from PIL import Image, ImageDraw
import io
import base64

# Page configuration
st.set_page_config(
    page_title="QR Code Generator",
    page_icon="πŸ“±",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Custom CSS for beautiful styling
st.markdown("""
<style>
    .main-header {
        text-align: center;
        padding: 2rem 0;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        border-radius: 10px;
        margin-bottom: 2rem;
    }
    
    .feature-box {
        background: #f8f9fa;
        padding: 1.5rem;
        border-radius: 10px;
        border-left: 4px solid #667eea;
        margin: 1rem 0;
    }
    
    .download-section {
        background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
        padding: 2rem;
        border-radius: 15px;
        text-align: center;
        margin: 2rem 0;
    }
    
    .sidebar-section {
        background: #ffffff;
        padding: 1rem;
        border-radius: 10px;
        margin: 1rem 0;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
</style>
""", unsafe_allow_html=True)

def create_qr_code(data, fill_color, back_color, border, box_size, error_correction, style_type):
    """Generate QR code with custom styling"""
    
    # Error correction levels
    error_levels = {
        'L (~7%)': qrcode.constants.ERROR_CORRECT_L,
        'M (~15%)': qrcode.constants.ERROR_CORRECT_M,
        'Q (~25%)': qrcode.constants.ERROR_CORRECT_Q,
        'H (~30%)': qrcode.constants.ERROR_CORRECT_H
    }
    
    # Create QR code instance
    qr = qrcode.QRCode(
        version=1,
        error_correction=error_levels[error_correction],
        box_size=box_size,
        border=border,
    )
    
    qr.add_data(data)
    qr.make(fit=True)
    
    # Style configurations
    if style_type == "Standard":
        img = qr.make_image(fill_color=fill_color, back_color=back_color)
    elif style_type == "Rounded":
        img = qr.make_image(
            image_factory=StyledPilImage,
            module_drawer=RoundedModuleDrawer(),
            fill_color=fill_color,
            back_color=back_color
        )
    elif style_type == "Circular":
        img = qr.make_image(
            image_factory=StyledPilImage,
            module_drawer=CircleModuleDrawer(),
            fill_color=fill_color,
            back_color=back_color
        )
    elif style_type == "Gradient":
        img = qr.make_image(
            image_factory=StyledPilImage,
            color_mask=RadialGradiantColorMask(),
            fill_color=fill_color,
            back_color=back_color
        )
    
    return img

def get_download_link(img, filename):
    """Generate download link for the QR code"""
    buffered = io.BytesIO()
    img.save(buffered, format="PNG")
    img_str = base64.b64encode(buffered.getvalue()).decode()
    href = f'<a href="data:image/png;base64,{img_str}" download="{filename}" class="download-btn">πŸ“₯ Download QR Code</a>'
    return href

# Main header
st.markdown("""
<div class="main-header">
    <h1>🎨 Beautiful QR Code Generator</h1>
    <p>Create stunning, customizable QR codes with advanced styling options</p>
</div>
""", unsafe_allow_html=True)

# Sidebar for customization
with st.sidebar:
    st.markdown("## πŸŽ›οΈ Customization Panel")
    
    # QR Code Content
    st.markdown('<div class="sidebar-section">', unsafe_allow_html=True)
    st.markdown("### πŸ“ Content")
    
    content_type = st.selectbox(
        "Content Type",
        ["Text", "URL", "Email", "Phone", "SMS", "WiFi"]
    )
    
    if content_type == "Text":
        qr_data = st.text_area("Enter your text:", height=100)
    elif content_type == "URL":
        qr_data = st.text_input("Enter URL:", placeholder="https://example.com")
    elif content_type == "Email":
        email = st.text_input("Email address:")
        subject = st.text_input("Subject (optional):")
        body = st.text_area("Message (optional):", height=80)
        qr_data = f"mailto:{email}?subject={subject}&body={body}" if email else ""
    elif content_type == "Phone":
        qr_data = st.text_input("Phone number:", placeholder="+1234567890")
        if qr_data:
            qr_data = f"tel:{qr_data}"
    elif content_type == "SMS":
        phone = st.text_input("Phone number:", placeholder="+1234567890")
        message = st.text_area("Message:", height=80)
        qr_data = f"sms:{phone}?body={message}" if phone else ""
    elif content_type == "WiFi":
        ssid = st.text_input("Network Name (SSID):")
        password = st.text_input("Password:", type="password")
        security = st.selectbox("Security Type", ["WPA", "WEP", "nopass"])
        hidden = st.checkbox("Hidden Network")
        if ssid:
            qr_data = f"WIFI:T:{security};S:{ssid};P:{password};H:{'true' if hidden else 'false'};;"
        else:
            qr_data = ""
    
    st.markdown('</div>', unsafe_allow_html=True)
    
    # Style Settings
    st.markdown('<div class="sidebar-section">', unsafe_allow_html=True)
    st.markdown("### 🎨 Style Settings")
    
    style_type = st.selectbox(
        "QR Style",
        ["Standard", "Rounded", "Circular", "Gradient"]
    )
    
    col1, col2 = st.columns(2)
    with col1:
        fill_color = st.color_picker("Foreground Color", "#000000")
    with col2:
        back_color = st.color_picker("Background Color", "#FFFFFF")
    
    st.markdown('</div>', unsafe_allow_html=True)
    
    # Advanced Settings
    st.markdown('<div class="sidebar-section">', unsafe_allow_html=True)
    st.markdown("### βš™οΈ Advanced Settings")
    
    box_size = st.slider("Module Size", 1, 20, 10)
    border = st.slider("Border Width", 1, 10, 4)
    error_correction = st.selectbox(
        "Error Correction Level",
        ["L (~7%)", "M (~15%)", "Q (~25%)", "H (~30%)"]
    )
    
    st.markdown('</div>', unsafe_allow_html=True)

# Main content area
col1, col2 = st.columns([2, 1])

with col1:
    if qr_data:
        try:
            # Generate QR code
            qr_img = create_qr_code(
                qr_data, fill_color, back_color, border, 
                box_size, error_correction, style_type
            )
            
            # Display QR code
            st.markdown("### πŸ“± Your QR Code")
            st.image(qr_img, caption="Generated QR Code", use_container_width=True)
            
            # Download section
            st.markdown("""
            <div class="download-section">
                <h3>πŸš€ Ready to Download!</h3>
                <p>Your QR code is ready. Click below to download it as PNG.</p>
            </div>
            """, unsafe_allow_html=True)
            
            # Download button
            filename = f"qrcode_{content_type.lower()}.png"
            st.markdown(get_download_link(qr_img, filename), unsafe_allow_html=True)
            
        except Exception as e:
            st.error(f"Error generating QR code: {str(e)}")
    else:
        st.info("πŸ‘ˆ Please enter content in the sidebar to generate your QR code")

with col2:
    st.markdown("### πŸ“‹ Features")
    
    features = [
        "🎨 Multiple visual styles",
        "🌈 Custom colors",
        "πŸ“± Various content types",
        "βš™οΈ Advanced settings",
        "πŸ“₯ Instant download",
        "πŸ”§ Error correction levels"
    ]
    
    for feature in features:
        st.markdown(f'<div class="feature-box">{feature}</div>', unsafe_allow_html=True)
    
    st.markdown("### πŸ’‘ Tips")
    st.info("""
    **Higher Error Correction** = More resilient to damage but larger QR code
    
    **Larger Module Size** = Easier to scan but bigger file
    
    **Good Contrast** = Better scanning reliability
    """)

# Footer
st.markdown("---")
st.markdown("""
<div style="text-align: center; color: #666; padding: 2rem;">
    <p>πŸš€ Built with Streamlit | πŸ’ Made with love for QR code enthusiasts</p>
</div>
""", unsafe_allow_html=True)