Custom QR Code Generator (with a Hyperdrive Mode)
Python, QR Codes, Automation, Batch Processing, Serious Fun
This intro video will explain the project. This first part of the project is free for everyone, but the final piece, Hyperdrive Mode, is behind the paywall for paid subscribers. Bulk processing and creating images is a useful skill and can be applied outside of the QR code concept.
QR Code, Code
import qrcode
from qrcode.image.pil import PilImage
# Define the URL for the QR code
#add your url, for example, https://ipchicken.com
url = 'your url here'
# Create a QR code object
qr = qrcode.QRCode(
version=1, # Controls the size of the QR Code (1 is the smallest)
error_correction=qrcode.constants.ERROR_CORRECT_L, # Error correction level
box_size=10, # Size of each box in the QR code
border=4, # Thickness of the border
)
# Add data to the QR code
qr.add_data(url)
qr.make(fit=True)
# Create an image from the QR code with custom colors
# You can change the colors here
img = qr.make_image(fill_color='blue', back_color='white', image_factory=PilImage)
# Save the image
#Change the file name to what you want
img.save('myqr_code.png')
#Confirms it works
print("QR code generated and saved'.")
QR Code with Text, Code
import qrcode
from qrcode.image.pil import PilImage
from PIL import Image, ImageDraw, ImageFont
# Define the URL for the QR code
url = 'your url here'
# Create a QR code object
qr = qrcode.QRCode(
version=1, # Controls the size of the QR Code (1 is the smallest)
error_correction=qrcode.constants.ERROR_CORRECT_L, # Error correction level
box_size=10, # Size of each box in the QR code
border=4, # Thickness of the border
)
# Add data to the QR code
qr.add_data(url)
qr.make(fit=True)
# Create an image from the QR code with custom colors
# You can change the colors here
qr_img = qr.make_image(fill_color='blue', back_color='black', image_factory=PilImage).convert('RGB')
# Load a font (using default)
font = ImageFont.load_default()
# Create a new image that includes space for the text
qr_width, qr_height = qr_img.size
total_height = qr_height + 40 # Adding extra height for the text
img_with_text = Image.new('RGB', (qr_width, total_height), color='black')
# Paste the QR code onto the new image
img_with_text.paste(qr_img, (0, 0, qr_width, qr_height))
# Initialize ImageDraw to add text
draw = ImageDraw.Draw(img_with_text)
text = "Home Page"
# Use textbbox to calculate the size of the text
text_bbox = draw.textbbox((0, 0), text, font=font)
text_width = text_bbox[2] - text_bbox[0]
text_height = text_bbox[3] - text_bbox[1]
# Position the text at the center below the QR code
text_position = ((qr_width - text_width) // 2, qr_height + 10)
draw.text(text_position, text, font=font, fill='white')
# Save the final image
# Choose the name you want
img_with_text.save('myqr_code_with_text.png')
print("QR code with text generated and saved.")
Why SUBSCRIBE? Price QR Generators that let you manage images in bulk. Do it yourself, and have code you can use for other jobs. Everything I am giving you will save you time, and hopfully you agree, that is worth a subscription.