Convert your POAP links.txt list into beautiful designs executing this simple Python scripts

gm POAP fam, here is 3 python scripts that you can use/run separately or together to get a beautiful design :hibiscus: from your links.txt list, the one that you get after creating your POAP.

First, here is a small summary of what is explained setp by step here:

  1. Create from all the links included in the “links.txt” file a unique and separated QR jpeg image for each one.
  2. Insert the obtained unique QRs images into your own beautiful template design.
  3. Create from all the obtained unique images the printing sheets ready to be printed and cutted.

The whole process has 3 steps that are resumed in this image. You can just run one part of the proceess if you want:

Some initial notes :nerd_face:

  • This tutorial is designed to be executed with python in the command line/shell of your own computer.
  • You must have python installed and ready to run in your command line/shell.
  • If you get a message that some library is missing, please come and ask here or use GPT / Google!

:white_check_mark: If you follow the steps, you don’t need to know anything about coding to run this successfully… but never run code that you can’t understand in your computer! :warning:

1. Transform the links listed in the links.txt file into QR separated jpeg images :gear:

With this script you will convert your links listed in your “links.txt” file into QR code jpeg images named consecutively from 1 to N (depending how many links do you have), all QRs jpeg will be unique and correspondant to each link.

To use it:

  1. Create the “generate-qrs.py” file in the same directory/filder your links.txt is.
  2. Install the required libraries on your local machine, execute: “pip install qrcode[pil]”
  3. To run the script execute: “python generate-qrs.py” and the QR codes should be generated.
import qrcode
from PIL import Image

def generate_qr(link, filename):
    # Create a QR instance
    qr = qrcode.QRCode(
        version=1,  # controls the size of the QR Code
        error_correction=qrcode.constants.ERROR_CORRECT_L,  # About 7% or less errors can be corrected
        box_size=12,  # size of each box in the QR grid
        border=2,  # Border size
    )

    # Add data to the QR instance
    qr.add_data(link)
    qr.make(fit=True)

    # Create an image from the QR instance
    img = qr.make_image(fill_color="black", back_color="white")

    dpi_value = 300  # Dots per inch for printing
    img = img.resize((int(4 * dpi_value / 2.54), int(4 * dpi_value / 2.54)))  # Resize to 4x4 cm at 300 dpi
    img.save(filename, 'JPEG', quality=95)

def main():
    with open("links.txt", "r") as file:
        links = file.readlines()
        for i, link in enumerate(links):
            link = link.strip()  # Remove any whitespace
            generate_qr(link, f"QR_{i+1}.jpeg")

if __name__ == "__main__":
    main()

You can customize the result size, quality and other small factors in the code, is commented.

QR code images will be named like this: “QR_N.jpeg” from 1 to N and will look like this:
QR_1000

Maybe this is enough for you, but if you want more automatization here it’s the next step.

2. Now you will insert the QRs into your beautiful TEMPLATE image :framed_picture:

With this script you will insert your QR codes into your template “cards” image (the one you have designed to get your QRs into them), the result “cards” will be named consecutively from 1 to N, depending how many QRs do you have, all “cards” will be unique.

To use it:

  1. Create the “generate-cards.py” file in the same directory/filder your “QR_N.jpeg” files are.
  2. Copy your “template.png” image to the same directory.
  3. To run the script execute: “python generate-cards.py” and the “cards” should be generated.
from PIL import Image

def place_qr_on_template(qr_path, template_path, output_path, top_left, bottom_right):
    # Open the template and QR image
    with Image.open(template_path) as template, Image.open(qr_path) as qr:
        # Resize the QR to fit into the specified square
        qr = qr.resize((bottom_right[0] - top_left[0], bottom_right[1] - top_left[1]))
        
        # Paste the QR code onto the template at the specified position
        template.paste(qr, top_left)
        
        # Save as PNG for lossless compression
        template.save(output_path, 'PNG')

def main():
    template_path = "template.png"
    top_left = (586, 145) #  The top left pixel where the QR code will be inserted
    bottom_right = (755, 312) # The bottom right where the QR code will be inserted
    
    # Here we configure the number of QR codes to process, for this example will be 1000 QR codes
    for i in range(1, 1001):
        qr_path = f"QR_{i}.jpeg"
        output_path = f"templated_QR_{i}.png"
        place_qr_on_template(qr_path, template_path, output_path, top_left, bottom_right)

if __name__ == "__main__":
    main()

You can customize the number of QR codes to be procceced, the location in the image, the quality and other small factors in the code, is commented.

The location is the most important part, so you need to pay atention to this part of the code, where you specify the TOP LEFT and the BOTTOM RIGHT of the QR code pixels to be inserted.

def main():
    template_path = "template.png"
    top_left = (586, 145)
    bottom_right = (755, 312)

This was my “template.png”, the file used by the script to generate the unique “cards” with the QR codes:

And here is the result example, the “card” images that will be named like this: “template_QR_N.png” from 1 to N and will look like this:

Maybe this is enough for you, but if you want more automatization here it’s the next step.

3. Now create the sheets ready for printing and cut :scissors:

With this script you will insert your “cards” into single sheets ready to print (you should take your measures and proportions. In my case Im doing 2 columns of 6 “cards” to use in a A4 printing page. The result printing sheets will be named consecutively from 1 to N as “combined_N.png”, depending how many “cards” do you have, all printing sheets will be unique.

To use it:

  1. Create the “generate-printing-sheets.py” file in the same directory/filder your “template_QR_N.png” files are.
  2. To run the script execute: “python generate-printing-sheets.py” and the printing sheets should be generated.
from PIL import Image

def combine_images(image_paths, output_path, rows, cols, gap): 
    # Open the images
    images = [Image.open(path) for path in image_paths]

    # Determine the size for the combined image
    img_width, img_height = images[0].size
    total_width = cols * img_width + (cols - 1) * gap
    total_height = rows * img_height + (rows - 1) * gap

    # Create a blank white image
    combined_img = Image.new("RGB", (total_width, total_height), "white")

    # Place each image onto the combined image
    for idx, img in enumerate(images):
        row = idx // cols
        col = idx % cols
        x = col * (img_width + gap)
        y = row * (img_height + gap)
        combined_img.paste(img, (x, y))

    combined_img.save(output_path)

def main():
    # Customizable parameters
    total_images = 1000
    rows = 6
    cols = 2
    gap = 5
    images_per_combined = rows * cols

    for i in range(0, total_images, images_per_combined):
        image_paths = [f"templated_QR_{j+1}.png" for j in range(i, i + images_per_combined) if j < total_images]
        output_path = f"combined_{i//images_per_combined + 1}.png"
        combine_images(image_paths, output_path, rows, cols, gap)

if __name__ == "__main__":
    main()

You can customize the number of raws, columns, margin and total cards to be procceced in this section of the code:

def main():
    # Customizable parameters
    total_images = 1000
    rows = 6
    cols = 2
    gap = 5
    images_per_combined = rows * cols

The result will depend on your images size, you should consider your sheet printing area and printer dpi as always, this is 100% up to you.

This was my printing sheet result:

Maybe this is enough for you, but if you want more go to learn some python or use GPT :nerd_face:

My X/Twitter: https://x.com/ariutokintumi

Feel free to ask any question :point_down:

3 Likes