[Python] How to perfectly image stitch and align images using Python, even if the images...

Discussão em 'Python' iniciado por Stack, Junho 21, 2025.

  1. Stack

    Stack Membro Participativo

    your text[​IMG]

    import os
    import cv2
    import numpy as np
    from PIL import Image
    from config import FOLDER1, FOLDER2, OUTPUT_FOLDER

    def stitch_images_and_save(index, df):
    """stitch images and save the result""

    try:
    files1 = sorted([f for f in os.listdir(FOLDER1) if f.lower().endswith((".png", ".jpg", ".jpeg"))])
    files2 = sorted([f for f in os.listdir(FOLDER2) if f.lower().endswith((".png", ".jpg", ".jpeg"))])
    imgs1 = files1[index:index+4]
    imgs2 = files2[index:index+4]
    images = []

    # Process first set of images
    for img_name in imgs1:
    img = Image.open(os.path.join(FOLDER1, img_name))
    # original_width, original_height = img.size
    # img = img.crop((610, 150, original_width - 100, original_height - 200))
    img = img.rotate(90, expand=True).resize((300, 250))
    images.append(img)

    # Process second set of images
    for img_name in imgs2:
    img = Image.open(os.path.join(FOLDER2, img_name))
    # original_width, original_height = img.size
    # img = img.crop((350, 95, original_width - 500, original_height - 253))
    img = img.rotate(90, expand=True).resize((300, 250))
    images.append(img)


    # Create a blank image
    width, height = 4 * 300, 2 * 250
    merged = Image.new("RGB", (width, height))

    for i in range(4):
    merged.paste(images, (i * 300, 0))
    for i in range(4, 8):
    merged.paste(images, ((i - 4) * 300, 250))

    # Save merged image
    chainage = df.iloc[index].get("Chainage", f"Image_{index}") if index < len(df) else f"Image_{index}"
    output_path = os.path.join(OUTPUT_FOLDER, f"{chainage}.jpg")
    merged.save(output_path)
    return output_path
    except Exception as e:
    print("Merge Error:", e)


    [​IMG]

    I want the stitch image to look like this image.You want to merge 8 images (4 from each folder) into a single image, arranged in a 2-row, 4-column grid with perfect vertical and horizontal alignment, so that:

    • Images don’t have any unwanted gaps or overlaps.
    • Images are visually aligned — both in size and position.
    • Any extra border/padding/cropping issues are handled before pasting.

    Continue reading...

Compartilhe esta Página