Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved code for the QR Generator #100

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions Beginner_Projects/QR Generator/QR-code_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import qrcode
from urllib.parse import urlparse
from PIL import ImageColor

# Function to validate the URL
def validate_url(url):
"""Validates the format of the URL."""
parsed_url = urlparse(url)
return all([parsed_url.scheme, parsed_url.netloc])

# Function to check if the color input is valid
def validate_color(color):
"""Validates the input color using the PIL library."""
try:
ImageColor.getrgb(color)
return True
except ValueError:
return False

# Function to get user input
def get_user_input():
"""Prompts the user for input and validates it."""
input_URL = input("Enter the URL: ")

# Validate URL input
if not validate_url(input_URL):
raise ValueError("Invalid URL format. Please enter a valid URL.")

# Get and validate color inputs
fill_color = input("Enter the fill color (default 'black'): ") or "black"
if not validate_color(fill_color):
raise ValueError(f"Invalid fill color '{fill_color}'. Please enter a valid color.")

back_color = input("Enter the background color (default 'white'): ") or "white"
if not validate_color(back_color):
raise ValueError(f"Invalid background color '{back_color}'. Please enter a valid color.")

# Get box size with basic validation
try:
box_size = int(input("Enter the box size (default 15): ") or 15)
if box_size <= 0:
raise ValueError("Box size must be a positive integer.")
except ValueError as ve:
print("Invalid box size. Using default (15).")
box_size = 15

return input_URL, fill_color, back_color, box_size

# Function to generate and save the QR code
def generate_qr_code(url, fill_color, back_color, box_size):
"""Generates a QR code with the given parameters and saves it as an image."""
try:
# Create QRCode instance
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=box_size,
border=4,
)
qr.add_data(url)
qr.make(fit=True)

# Create and save the QR code image
img = qr.make_image(fill_color=fill_color, back_color=back_color)
img.save("url_qrcode.png")
print(f"QR code saved as 'url_qrcode.png' for URL: {url}")
print(qr.data_list)
except Exception as e:
raise RuntimeError(f"Error generating the QR code: {e}")

# Main function to run the application
def main():
"""Main entry point to run the QR code generator."""
try:
# Get user input and generate QR code
input_URL, fill_color, back_color, box_size = get_user_input()
generate_qr_code(input_URL, fill_color, back_color, box_size)
except ValueError as ve:
print(f"Input Error: {ve}")
except RuntimeError as re:
print(f"Runtime Error: {re}")
except Exception as e:
print(f"An unexpected error occurred: {e}")

# Run the program
if __name__ == "__main__":
main()