Commit 962a4c97 authored by Michele Maris's avatar Michele Maris
Browse files

Merge branch 'main' of https://www.ict.inaf.it/gitlab/michele.maris/yapsut into main

parents a830f964 8ca4eaa2
Loading
Loading
Loading
Loading
+93 −0
Original line number Diff line number Diff line
from PIL import Image

def collate_images_r1_c2(image1_path, image2_path, output_path):
    """ collates 2 png images of equal shape side by side in a (1 row,2 columns) scheme"""
    # 1. Open both PNG images
    img1 = Image.open(image1_path)
    img2 = Image.open(image2_path)

    # 2. Get the dimensions of both images
    width1, height1 = img1.size
    width2, height2 = img2.size

    # 3. Calculate the dimensions of the new canvas
    total_width = width1 + width2
    max_height = max(height1, height2)

    # 4. Create a new blank image with 'RGBA' mode to preserve transparency
    new_image = Image.new('RGBA', (total_width, max_height))

    # 5. Paste the first image at the far left (coordinates 0, 0)
    new_image.paste(img1, (0, 0))

    # 6. Paste the second image right next to it (coordinates width1, 0)
    new_image.paste(img2, (width1, 0))

    # 7. Save the combined result as a new PNG
    new_image.save(output_path, format='PNG')
    return output_path

def collate_images_r2_c1(image1_path, image2_path, output_path):
    """ collates 2 png images of equal shape side by side in a (2 rows,1 column) scheme"""
    # 1. Open both PNG images
    img1 = Image.open(image1_path)
    img2 = Image.open(image2_path)

    # 2. Get the dimensions of both images
    width1, height1 = img1.size
    width2, height2 = img2.size

    # 3. Calculate the dimensions of the new canvas
    width = max(width1, width2)
    height = height1+height2

    # 4. Create a new blank image with 'RGBA' mode to preserve transparency
    new_image = Image.new('RGBA', (width, height))

    # 5. Paste the first image at the far left (coordinates 0, 0)
    new_image.paste(img1, (0, 0))

    # 6. Paste the second image right next to it (coordinates 0, height1)
    new_image.paste(img2, (0, height1))

    # 7. Save the combined result as a new PNG
    new_image.save(output_path, format='PNG')
    return output_path

def collate_images_r2_c2(image1_path, image2_path, image3_path, image4_path, output_path):
    """ collates 4 png images of equal shape side by side in a (2 rows,2 column) scheme"""
    # 1. Open both PNG images
    img1 = Image.open(image1_path)
    img2 = Image.open(image2_path)
    img3 = Image.open(image3_path)
    img4 = Image.open(image4_path)

    # 2. Get the dimensions of both images
    width1, height1 = img1.size
    width2, height2 = img2.size
    width3, height3 = img3.size
    width4, height4 = img4.size

    # 3. Calculate the dimensions of the new canvas
    hwidth = max(width1, width2,width3, width4)
    hheight = max(height1, height2,height3, height4)

    # 4. Create a new blank image with 'RGBA' mode to preserve transparency
    new_image = Image.new('RGBA', (2*hwidth, 2*hheight))

    # 5. Paste the first image at the far left (coordinates 0, 0)
    new_image.paste(img1, (0, 0))

    # 5. Paste the second image right next to it (coordinates 0, height1)
    new_image.paste(img2, (hwidth, 0))

    # 5. Paste the third image right next to it (coordinates 0, height1)
    new_image.paste(img3, (0, hheight))


    # 5. Paste the fourth image right next to it (coordinates 0, height1)
    new_image.paste(img3, (hwidth, hheight))

    # 7. Save the combined result as a new PNG
    new_image.save(output_path, format='PNG')
    return output_path