Skip to content
Closed
Show file tree
Hide file tree
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
19 changes: 19 additions & 0 deletions 100lines Generator/Qr/README.md
Original file line number Diff line number Diff line change
@@ -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.
85 changes: 22 additions & 63 deletions 100lines Generator/Qr/qr_generator.py
Original file line number Diff line number Diff line change
@@ -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():
Expand All @@ -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__":
Expand Down
1 change: 1 addition & 0 deletions 100lines Generator/Qr/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
qrcode[pil]
Loading