Skip to content

Instantly share code, notes, and snippets.

@mralext20
Last active May 3, 2024 14:18
Show Gist options
  • Save mralext20/03fbfbc33885ccb3a1a46629fd82a672 to your computer and use it in GitHub Desktop.
Save mralext20/03fbfbc33885ccb3a1a46629fd82a672 to your computer and use it in GitHub Desktop.
generates QR codes with letter and number pairs, saves to a PDF
from PIL import Image
from PIL import ImageDraw
from qrcode.main import QRCode
from qrcode.constants import ERROR_CORRECT_L
from PIL import ImageFont
from typing import Sequence, Generator, TypeVar
import math
_T = TypeVar("_T")
def grouper(iterable: Sequence[_T], n: int) -> Generator[Sequence[_T], None, None]:
"""
given a iterable, yield that iterable back in chunks of size n. last item will be any size.
"""
for i in range(math.ceil(len(iterable) / n)):
yield iterable[i * n : i * n + n]
biggerFont = ImageFont.truetype("font.ttf", 500)
smallerFont = ImageFont.truetype("font.ttf", 250)
smallestFont = ImageFont.truetype("font.ttf", 150)
requests = []
aisles = {"MD1": 25, "GR1": 25, "GM1": 20, "FT1": 17, "FT2": 20, "BK1": 30}
aisles = {"A": 8, "B": 8, "C": 8, "D": 8, "E": 14, "F": 12, "G": 12, "H": 12, "I": 16, "J": 10}
for letter, count in aisles.items(): # 10 rows of steel
for number in range(count): # 16 sections max
requests.append((letter, number + 1))
images = []
for index, group in enumerate(grouper(requests, 4)):
# Create an image object with the desired dimensions
img = Image.new("RGB", (2200, 1700), color=(255, 255, 255))
# 2200 / 2 -> 1100 per verticle
# 1700 / 2 -> 850 per horizontal
print(f"{index=}, {group=}")
for i in range(2):
for j in range(2):
if len(group) == 0:
break
this_group = group.pop(0)
letter = str(this_group[0])
number = str(this_group[1])
qr = QRCode(
version=1,
error_correction=ERROR_CORRECT_L,
box_size=50,
border=0,
)
qr.add_data(f"{letter}-{number}")
qr.make(fit=True)
qr_img = qr.make_image(fill_color="black", back_color="white")
img.paste(
qr_img.resize((750, 750)),
((j * 1100 + 50), (i * 850) + 50),
)
if len(letter) == 3:
ImageDraw.Draw(img).text(((j * 1100) + 810, (i * 850) + 250), letter, font=smallestFont, fill="black")
else:
ImageDraw.Draw(img).text(((j * 1100) + 810, (i * 850) - 100), letter, font=biggerFont, fill="black")
if len(number) == 2:
ImageDraw.Draw(img).text(((j * 1100) + 810, (i * 850) + 450), number, font=smallerFont, fill="black")
else:
ImageDraw.Draw(img).text(((j * 1100) + 810, (i * 850) + 300), number, font=biggerFont, fill="black")
# Save the image
images.append(img)
img.save(f"out_{index}.jpg")
images[0].save("out_all.pdf", save_all=True, append_images=images[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment