GroupDocs.Merger for Python via .NET allows you to stack multiple images into a single output image programmatically. Use ImageJoinOptions to control the output format and the direction in which images are joined — horizontally side by side or vertically one above the other.
The library supports JPG, PNG, BMP, and other common image formats.
Steps to merge JPEG images vertically
Create an instance of the Merger class and pass the path to the first (base) image.
Create an ImageJoinOptions instance specifying the target FileType and the ImageJoinMode.
Call merger.join() with the path to each additional image and the join options.
Call merger.save() with the desired output path.
fromgroupdocs.mergerimportMergerfromgroupdocs.merger.domainimportFileTypefromgroupdocs.merger.domain.optionsimportImageJoinOptions,ImageJoinModedefmerge_image_documents():# Load the first image as the merge basewithMerger("./input.jpg")asmerger:# Configure join options: output as JPEG, stack images verticallyimage_join_options=ImageJoinOptions(FileType.JPEG,ImageJoinMode.VERTICAL)# Append the second image with the join options appliedmerger.join("./input2.jpg",image_join_options)# Save the combined imagemerger.save("./output.jpg")if__name__=="__main__":merge_image_documents()
input.jpg is a sample file used in this example. Click here to download it.
input2.jpg is a sample file used in this example. Click here to download it.
Explanation
Load base image: Merger("./input.jpg") opens the first image as the merge base inside a context manager.
ImageJoinOptions(FileType.JPEG, ImageJoinMode.VERTICAL): Specifies that the output should be a JPEG file and that images should be stacked vertically (top to bottom). Use ImageJoinMode.HORIZONTAL to place them side by side instead.
Append second image: merger.join("./input2.jpg", image_join_options) appends the second image according to the join options.
Save result: merger.save("./output.jpg") writes the merged image to disk.