Created
December 11, 2024 15:33
-
-
Save madhurprash/80a98fd330e48378eba4027b92fc08a1 to your computer and use it in GitHub Desktop.
This gist takes in a list of links and generates QRCodes for those links.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import qrcode | |
| import logging | |
| import re | |
| import os | |
| # Install the following package before running the script: pip install qrcode | |
| # set a logger | |
| logging.basicConfig( | |
| format='[%(asctime)s] p%(process)s {%(filename)s:%(lineno)d} %(levelname)s - %(message)s', | |
| level=logging.INFO | |
| ) | |
| logger = logging.getLogger(__name__) | |
| # List of URLs to generate QR codes for | |
| urls = [ | |
| "https://github.com/aws-samples/foundation-model-benchmarking-tool" | |
| ] | |
| # A helper function to create a safe filename from the URL | |
| def url_to_filename(url): | |
| # Remove protocols and non-alphanumeric chars to form a safe filename base | |
| base = re.sub(r'[^0-9a-zA-Z]+', '_', url) | |
| # Limit length and ensure it doesn't result in an empty string | |
| return base[:50] if base else "qrcode" | |
| for i, url in enumerate(urls, start=1): | |
| qr = qrcode.QRCode( | |
| version=1, | |
| error_correction=qrcode.constants.ERROR_CORRECT_L, | |
| box_size=10, | |
| border=4, | |
| ) | |
| qr.add_data(url) | |
| qr.make(fit=True) | |
| img = qr.make_image(fill_color="black", back_color="white") | |
| # Generate a filename for the QR code image | |
| filename = url_to_filename(url) | |
| filename = f"{filename}.png" | |
| # Ensure unique filenames in case of duplicates | |
| counter = 1 | |
| original_filename = filename | |
| while os.path.exists(filename): | |
| filename = f"{os.path.splitext(original_filename)[0]}_{counter}.png" | |
| counter += 1 | |
| img.save(filename) | |
| logger.info(f"QR code generated and saved as {filename}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment