Griddler

 
A Python script that takes a collection of images and grids them into a single image
 
 

What is Griddler?

I’ve been doing some work on my website and found that showing an entire collection of work with a single, low file-size image can be really helpful. I’m incredibly fast at Photoshop and love Adobe’s batch processors, but nothing makes a simple grid really quickly. So I did!

If you’re a photographer or use Photoshop and Illustrator a lot but don’t use Python with the Pillow/PILL library, hit up your local ChatGPT and check it out. It does a lot of cool stuff, like make these grids I’m talking about!

So download my script below and open it in your IDE of choice (I use VSCode) and start griddlin’!

INSTRUCTIONS:

  • Define your Source folder and Destination folder.

  • Define the new size and grid layout.

    • Ensure all images in the collection are of identical dimensions for the script to function properly. The images can be of any size or aspect ratio, as long as each image within the collection shares the same dimensions.

  • Define the background color for your grid (in case your images are transparent.

  • Change name of grid.

Don’t worry about any of the other steps in between. Click run and you’re in the grid biz!

 
 
 
 

ExampleS

 
 
 

The Code

The neat thing about codes like this one is I can just copy and paste it below. That way if anything happens to the downloadable version, you can still get it here:

from PIL import Image
import os

# STEP 1 <-------
# Define your source and destination folders
source_folder = r'COPY AND PASTE YOUR SOURCE FOLDER PATH HERE'
destination_folder = r'COPY AND PASTE YOUR DESTINATION FOLDER PATH HERE'

# Make sure the destination folder exists
os.makedirs(destination_folder, exist_ok=True)

# STEP 2 <-------
# Define the new size and grid layout
new_size = (100, 100)  # This is the size of each image in the grid (W,H)
grid_size = (5, 10)  # This is the number of images in the grid (W,H)

# STEP 3 <-------
# Define the background color for the grid and for transparent images
background_color = (255, 255, 255)

# Initialize an empty image with the specified background color for the grid
grid_image = Image.new('RGB', (new_size[0] * grid_size[0], new_size[1] * grid_size[1]), background_color)

# Get the list of image files in the source directory
image_files = [f for f in os.listdir(source_folder) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))]
image_files.sort()  # Sort the files to keep the order consistent

# Check if we have the right number of images
if len(image_files) != grid_size[0] * grid_size[1]:
    raise ValueError(f"Expected {grid_size[0] * grid_size[1]} images, but found {len(image_files)}.")

# Loop through the images and place them in the grid
for index, image_file in enumerate(image_files):
    row = index // grid_size[0]
    col = index % grid_size[0]
    position = (col * new_size[0], row * new_size[1])

    with Image.open(os.path.join(source_folder, image_file)).convert("RGBA") as img:
        # Create a solid background image of the same size as the transparent image
        background = Image.new("RGB", img.size, background_color)
        # Paste the transparent image onto the background
        background.paste(img, (0, 0), img)  # Using img as the mask for transparency
        resized_img = background.resize(new_size, Image.Resampling.LANCZOS)
        grid_image.paste(resized_img, position)

# STEP 4 <-------
# Save the final grid image
grid_image.save(os.path.join(destination_folder, 'CHANGE-NAME-OF-GRID-HERE.png'), 'PNG')
print("Grid image created successfully.")