How to Composite Two Images Together using Python

Cloudmersive
3 min readJul 28, 2022

--

Ever seen a picture of a Giraffe walking around in New York City unattended? Or perhaps you’ve seen a photo of your friend standing on the surface of some far-off planet, studying their lifeless surroundings? Unless your friend is an extraterrestrial being, chances are that you were looking at a composite image — one in which two separate images were seamlessly combined to create a mindfully surreal effect. These images can be a challenge to create, but thankfully there’s an API for that. Our Image Composite API will layer two images together — including a “base image,” which serves as the foundation for the composite, and a “layered image,” which imposes a new subject on top of the base. Additionally, this API allows you to specify the general location where the composite should take place on the base image, with options including “center,” “top left,” “top-center,” and so-on.

You can take advantage of this API for free by registering a free-tier account on our website, and then following instructions below to structure your API call in Python (additional code examples are available in 12+ additional programming languages on our API console page).

Without further ado, let’s begin installing the Python SDK by running the below command:

pip install cloudmersive-image-api-client

After that, let’s include the imports:

from __future__ import print_function
import time
import cloudmersive_image_api_client
from cloudmersive_image_api_client.rest import ApiException
from pprint import pprint

Then, let’s authorize our API key in the relevant snippet below before configuring our parameters and calling the function:

# Configure API key authorization: Apikey
configuration = cloudmersive_image_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
# create an instance of the API class
api_instance = cloudmersive_image_api_client.EditApi(cloudmersive_image_api_client.ApiClient(configuration))
location = 'location_example' # str | Location to composite the layered images; possible values are: \"center\", \"top-left\", \"top-center\", \"top-right\", \"center-left\", \"center-right\", \"bottom-left\", \"bottom-center\", \"bottom-right\"
base_image = '/path/to/inputfile' # file | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
layered_image = '/path/to/inputfile' # file | Image to layer on top of the base image.
try:
# Composite two images together
api_response = api_instance.edit_composite_basic(location, base_image, layered_image)
pprint(api_response)
except ApiException as e:
print("Exception when calling EditApi->edit_composite_basic: %s\n" % e)

That just about wraps it up.

But wait — there’s actually more. If you’re looking to get a little more precision out of this operation, you can instead call the following function & configure the layered image based on specific measurements in pixels:

# create an instance of the API class
api_instance = cloudmersive_image_api_client.EditApi(cloudmersive_image_api_client.ApiClient(configuration))
base_image = '/path/to/inputfile' # file | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
layered_image = '/path/to/inputfile' # file | Image to layer on top of the base image.
top = 56 # int | Optional; Desired distance in pixels from the top of the base image to the top of the layered image. (optional)
bottom = 56 # int | Optional; Desired distance in pixels from the bottom of the base image to the bottom of the layered image. (optional)
left = 56 # int | Optional; Desired distance in pixels from the left side of the base image to the left side of the layered image. (optional)
right = 56 # int | Optional; Desired distance in pixels from the right side of the base image to the right side of the layered image. (optional)
width = 56 # int | Optional; Desired width of the layered image in pixels. Leave height empty or 0 to automatically scale the image proportionally. (optional)
height = 56 # int | Optional; Desired height of the layered image in pixels. Leave width empty or 0 to automatically scale the image proportionally. (optional)
try:
# Composite two images together precisely
api_response = api_instance.edit_composite_precise(base_image, layered_image, top=top, bottom=bottom, left=left, right=right, width=width, height=height)
pprint(api_response)
except ApiException as e:
print("Exception when calling EditApi->edit_composite_precise: %s\n" % e)

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet