how to stack input *.tif files to a new output.tif raster file [closed]

Solution for how to stack input *.tif files to a new output.tif raster file [closed]
is Given Below:

I want to stack raster files (.tiff) and create a new raster (.tiff).
The input raster files are RGB and need to be combined without color change.
Can anyone help me to stack them and combine a new RGB file?

Example to clear things up:
inputfile1.tif contains a red A
inputfile2.tif contains a blue C
inputfile3.tif contains a green S
… …
inputfile15.tif contains a purple X

outputfile.tif contains red A, blue C, green S, …, purple X

This is how I want to stack the raster data

Assuming you have a folder named tiffs with the following images:





The following script uses the third-party module PIL to open your .tif images, and generates a composite image. You didn’t specify how they should be stitched together, so this script just puts them next to each other in a single row, from left to right. It inserts them in the order in which pathlib.Path.glob discovered the image file paths – in this case in lexicographical order (that’s why I added a leading zero to the file names where normally there would be just a single digit).

def main():

    from PIL import Image
    from pathlib import Path
    from itertools import accumulate

    paths = Path("tiffs").glob("*.tif")

    images = list(map(, paths))

    offsets = list(accumulate((image.size[0] for image in images), initial=0))

    width = offsets[-1]
    height = max(image.size[1] for image in images)

    composite ="RGB", (width, height))

    for x_offset, image in zip(offsets, images):
        composite.paste(image, (x_offset, 0))
    return 0

if __name__ == "__main__":
    import sys


This should work for any number of images, with any file name.