How to merge multiple PNG files together in Python

Using an API and a few simple lines of code, we will be able to merge PNG files together in no time!

We will need to install our client first:

pip install cloudmersive-convert-api-client

And now to set up our function call. merge_document_png_multi will need an API instance, which we can create using a free key. We can input up to 10 files at a time for our merge.

from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom cloudmersive_convert_api_client.rest import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_convert_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_convert_api_client.MergeDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))input_file1 = '/path/to/file' # file | First input file to perform the operation on.input_file2 = '/path/to/file' # file | Second input file to perform the operation on.input_file3 = '/path/to/file' # file | Third input file to perform the operation on. (optional)input_file4 = '/path/to/file' # file | Fourth input file to perform the operation on. (optional)input_file5 = '/path/to/file' # file | Fifth input file to perform the operation on. (optional)input_file6 = '/path/to/file' # file | Sixth input file to perform the operation on. (optional)input_file7 = '/path/to/file' # file | Seventh input file to perform the operation on. (optional)input_file8 = '/path/to/file' # file | Eighth input file to perform the operation on. (optional)input_file9 = '/path/to/file' # file | Ninth input file to perform the operation on. (optional)input_file10 = '/path/to/file' # file | Tenth input file to perform the operation on. (optional)try:# Merge Multple PNG Files Togetherapi_response = api_instance.merge_document_png_multi(input_file1, input_file2, input_file3=input_file3, input_file4=input_file4, input_file5=input_file5, input_file6=input_file6, input_file7=input_file7, input_file8=input_file8, input_file9=input_file9, input_file10=input_file10)pprint(api_response)except ApiException as e:print("Exception when calling MergeDocumentApi->merge_document_png_multi: %s\n" % e)

Done! If you have more than 10 files, simple call the function again as many times as you need, using the previous output as file1 to maintain the order. Within this library, you can also find functions for merging other file formats, such as DOCX and PDF, and a whole lot of other useful stuff!

Image for post
Image for post

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store