Convert Input Image to JPG, JPEG Format in Python
Switching up image file formats can be important depending on the application or platform you’re using to process or upload the image. Using the /image/convert/to/jpg iteration of the Cloudmersive Image API, you can easily convert an image to a JPG/JPEG format in a few simple steps. This API accepts a wide variety of input file formats & can be connected to in 13+ common programming languages. In this article., we’ll walk through how you can connect in Python.
To start the process, first use the below command to install the Python SDK:
pip install cloudmersive-image-api-client
Next, start off the callback function with the below code. The second snippet included here will authenticate your Cloudmersive API key, so make sure you have one at this stage (if not, you can easily get one by making a free account on the Cloudmersive website):
from __future__ import print_function
import time
import cloudmersive_image_api_client
from cloudmersive_image_api_client.rest import ApiException
from pprint import pprint# Configure API key authorization: Apikey
configuration = cloudmersive_image_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
Finally, use the remaining code to complete the callback function and get your image converted:
# create an instance of the API class
api_instance = cloudmersive_image_api_client.ConvertApi(cloudmersive_image_api_client.ApiClient(configuration))
quality = 56 # int | Set the JPEG quality level; lowest quality is 1 (highest compression), highest quality (lowest compression) is 100; recommended value is 75
image_file = '/path/to/inputfile' # file | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.try:
# Convert input image to JPG, JPEG format
api_response = api_instance.convert_to_jpg(quality, image_file)
pprint(api_response)
except ApiException as e:
print("Exception when calling ConvertApi->convert_to_jpg: %s\n" % e)