QR Code Generator Web Application with Streamlit

 

Introduction:

This simple web application built using Streamlit allows users to generate and download QR codes. The application lets users input a link and a filename and then generates a QR code image that can be displayed and downloaded.

Project Prerequisites:

Before running the QR Code Generator project, ensure you have the following prerequisites set up on your system:

  1. Python: Make sure Python is installed on your system. You can download and install Python from the official website.
  2. Streamlit: Streamlit is a Python library used for building interactive web applications. You can install it via pip:
pip install streamlit
  1. qrcode: A Python library for generating QR codes. You can install it via pip:
pip install qrcode[pil]
  1. Pillow: A Python Imaging Library (PIL) fork that provides image processing capabilities. You can install it via pip:
pip install pillow

Getting Started:

Follow these steps to set up and run the QR Code Generator project:

Step 1: Install Python and Required Libraries

  1. If you haven’t already, download and install Python from the official website.
  2. Open your command line interface (CLI) or terminal.
  3. Install the required libraries by running the following commands:
pip install streamlit
pip install qrcode[pil]
pip install pillow

Step 2: Create a New Python Script

  1. Open your preferred text editor or Integrated Development Environment (IDE).
  2. Create a new Python script file, e.g., qr_code_generator.py.

Step 3: Write the Code

Copy and paste the following code into your Python script file:

import streamlit as st
import qrcode
from PIL import Image

def generate_qr_code(link, filename):
qr = qrcode.QRCode(
version=1, # QR code version (1 to 40, higher means more data capacity)
error_correction=qrcode.constants.ERROR_CORRECT_L, # Error correction level ('L', 'M', 'Q', 'H')
box_size=10, # Pixel size of each box in the QR code
border=4, # Border size around the QR code (in boxes)
)
qr.add_data(link)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img.save(filename)

def main():
st.title("QR Code Generator")
link = st.text_input("Enter the link:")
filename = st.text_input("Enter Image Name:")

if st.button("Generate QR Code"):
if link and filename:
generate_qr_code(link, f"{filename}.png")
st.success("QR Code generated successfully!")
# Display the generated QR code
qr_img = Image.open(f"{filename}.png")
st.image(qr_img, caption="Generated QR Code", use_column_width=True)
else:
st.error("Please enter both link and filename.")

if st.button("Download QR Code"):
if filename:
try:
generate_qr_code(link, f"{filename}.png")
with open(f"{filename}.png", "rb") as file:
st.download_button(label="Download QR Code", data=file, file_name=f"{filename}.png", mime="image/png")
except Exception as e:
st.error(f"An error occurred: {e}")
else:
st.error("Please enter a filename before downloading.")

if __name__ == "__main__":
main()

Step 4: Run the Application

  1. Save the Python script file.
  2. Open your CLI or terminal and navigate to the directory where your Python script is saved.
  3. Run the following command to start the Streamlit application:
streamlit run qr_code_generator.py

Step 5: Use the QR Code Generator

  1. Once the Streamlit application is running, you’ll see input fields to enter the link and filename.
  2. Enter the desired link and filename, then click the “Generate QR Code” button to create the QR code.
  3. The generated QR code will be displayed on the main page.
  4. To download the QR code, click the “Download QR Code” button.

That’s it! You’ve successfully set up and used the QR Code Generator project. Feel free to customize the code or add additional features to enhance the application further.

Advertisement