How to change the DPI of a photo in Python

Today we will use our convert API to easily change the DPI of an image file. Let’s begin by installing the client using pip installer.

pip install git+https://github.com/Cloudmersive/Cloudmersive.APIClient.Python.Convert.git

All that’s left is to call convert_image_image_set_dpi and input the image file and specify the desired DPI:

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'
# 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 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/file' # 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)

And it’s really just that simple.

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