Skip to content

Instantly share code, notes, and snippets.

@kolibril13
Last active July 20, 2025 12:29
Show Gist options
  • Save kolibril13/5e241265e8823b982199f294cc2e0084 to your computer and use it in GitHub Desktop.
Save kolibril13/5e241265e8823b982199f294cc2e0084 to your computer and use it in GitHub Desktop.
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "qrcode[pil]",
# "pillow",
# ]
# ///
import qrcode
from PIL import Image
# URL to encode in the QR code
url = "https://gist.github.com/kolibril13/5e241265e8823b982199f294cc2e0084"
# Create QR code instance
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
# Add data to the QR code
qr.add_data(url)
qr.make(fit=True)
# Create QR code image with white background first
qr_img = qr.make_image(fill_color="black", back_color="white")
# Convert to RGBA mode to support transparency
qr_img = qr_img.convert("RGBA")
# Make white pixels transparent
data = qr_img.getdata()
new_data = []
for item in data:
# Change all white (255, 255, 255) pixels to transparent
if item[:3] == (255, 255, 255):
new_data.append((255, 255, 255, 0)) # Transparent
else:
new_data.append(item)
qr_img.putdata(new_data)
# Save the QR code as PNG with transparency
qr_img.save("qr_code_transparent.png", "PNG")
print(f"QR code generated for URL: {url}")
print("Saved as: qr_code_transparent.png")
print("The QR code has a transparent background.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment