From 3aa204d4562f5ebc5ac0ec199a60a60c5d0e7065 Mon Sep 17 00:00:00 2001 From: Arnav Alok Date: Sat, 26 Sep 2026 13:22:22 +0530 Subject: [PATCH] Add QR code PNG generator --- 100lines Generator/Qr/README.md | 19 ++++++ 100lines Generator/Qr/qr_generator.py | 85 +++++++------------------- 100lines Generator/Qr/requirements.txt | 1 + 3 files changed, 42 insertions(+), 63 deletions(-) create mode 100644 100lines Generator/Qr/README.md create mode 100644 100lines Generator/Qr/requirements.txt diff --git a/100lines Generator/Qr/README.md b/100lines Generator/Qr/README.md new file mode 100644 index 00000000..f19a3de8 --- /dev/null +++ b/100lines Generator/Qr/README.md @@ -0,0 +1,19 @@ +# QR Code Generator + +Generate a QR code from any text or URL and save it as a PNG image in the +user's `Downloads` folder. + +## Setup + +```bash +python3 -m pip install -r requirements.txt +``` + +## Usage + +```bash +python3 qr_generator.py +``` + +Enter the data when prompted. The generated `qr_code_*.png` file can be opened +or shared like any other photo. diff --git a/100lines Generator/Qr/qr_generator.py b/100lines Generator/Qr/qr_generator.py index 8e9ef423..f9c96cc3 100644 --- a/100lines Generator/Qr/qr_generator.py +++ b/100lines Generator/Qr/qr_generator.py @@ -1,55 +1,36 @@ #!/usr/bin/env python3 -""" -QR Code Generator Utility +"""Generate a QR code from user input and save it as a PNG image.""" -Generates a QR code from user-defined data and saves it as a PNG file -in the user's Downloads folder. - -Dependencies: - pip install qrcode[pil] - -Usage: - python qr_generator.py -""" - -import os -import sys from datetime import datetime +from pathlib import Path + try: - import qrcode: + import qrcode except ImportError: print("Module 'qrcode' not found. Please install it with: pip install qrcode[pil]") - sys.exit(1) + raise SystemExit(1) def get_downloads_folder(): - """Return the path to the user's Downloads folder in a cross-platform way.""" - if sys.platform.startswith('win'): - import ctypes.wintypes + """Return the user's Downloads folder, creating it when necessary.""" + downloads = Path.home() / "Downloads" + downloads.mkdir(parents=True, exist_ok=True) + return downloads - # SHGetKnownFolderPath constants - CSIDL_PERSONAL = 0x0005 # My Documents - SHGFP_TYPE_CURRENT = 0 - try: - from ctypes import windll, create_unicode_buffer - buf = create_unicode_buffer(260) - # 0x000c is CSIDL for Downloads folder (CSIDL_DOWNLOADS) - # Windows Vista and later supports KnownFolder API better but fallback here - # For simplicity, try the environment variable fallback below instead +def generate_qr(data): + """Create a PNG QR code and return its path.""" + if not data.strip(): + raise ValueError("Data cannot be empty.") - # If this method is unreliable, fallback to env var: - downloads = os.path.join(os.environ['USERPROFILE'], 'Downloads') - if os.path.exists(downloads): - return downloads - except Exception: - pass - # fallback - return os.path.join(os.path.expanduser('~'), 'Downloads') - - else: - # macOS and Linux generally have ~/Downloads - return os.path.join(os.path.expanduser('~'), 'Downloads') + filename = f"qr_code_{datetime.now():%Y%m%d_%H%M%S}.png" + filepath = get_downloads_folder() / filename + qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L, + box_size=10, border=4) + qr.add_data(data) + qr.make(fit=True) + qr.make_image(fill_color="black", back_color="white").save(filepath, format="PNG") + return filepath def main(): @@ -60,29 +41,7 @@ def main(): print("No data entered. Exiting.") return - downloads_path = get_downloads_folder() - if not os.path.exists(downloads_path): - print(f"Downloads folder not found at expected location: {downloads_path}") - print("Saving in current directory instead.") - downloads_path = os.getcwd() - - timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") - filename = f"qr_code_{timestamp}.png" - filepath = os.path.join(downloads_path, filename) - - qr = qrcode.QRCode( - version=1, - error_correction=qrcode.constants.ERROR_CORRECT_L, - box_size=10, - border=4, - ) - qr.add_data(data) - qr.make(fit=True) - - img = qr.make_image(fill_color="black", back_color="white") - img.save(filepath) - - print(f"QR code saved to: {filepath}") + print(f"QR code saved to: {generate_qr(data)}") if __name__ == "__main__": diff --git a/100lines Generator/Qr/requirements.txt b/100lines Generator/Qr/requirements.txt new file mode 100644 index 00000000..e4101d57 --- /dev/null +++ b/100lines Generator/Qr/requirements.txt @@ -0,0 +1 @@ +qrcode[pil] \ No newline at end of file