How to Change Image DPI, Resize Image using Python

Cloudmersive
2 min readJan 23, 2023

--

The resolution quality of an image boils down to its DPI (dots per inch; expressed in terms of pixels per inch), and this value can be manipulated easily. Using the API solution below, you can quickly resize an image in your Python workflow by programmatically changing its DPI to a new integer value of your choosing. This API is free to use, and you can structure your API call using the ready-to-run code provided in this article.

Your first step is to install the SDK by running the below command:

pip install cloudmersive-convert-api-client

Next, call the function (starting with the imports) below:

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

# Configure API key authorization: Apikey
configuration = cloudmersive_convert_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'



# create an instance of the API class
api_instance = cloudmersive_convert_api_client.ConvertImageApi(cloudmersive_convert_api_client.ApiClient(configuration))
dpi = 56 # int | New DPI in pixels-per-inch, for example 300 DPI or 600 DPI
input_file = '/path/to/inputfile' # file | Input file to perform the operation on.

try:
# Change image DPI
api_response = api_instance.convert_image_image_set_dpi(dpi, input_file)
pprint(api_response)
except ApiException as e:
print("Exception when calling ConvertImageApi->convert_image_image_set_dpi: %s\n" % e)

To use this API for free, register a free account on our website and copy your free-tier API key into the configuration.api_key field above. Then you’re all done — simple as that.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet