How to change the DPI of a photo in Ruby

Today let’s take a quick look at how to adjust the DPI or PPI of an image file using Ruby and one of our APIs. To begin the process we add our client to the Gemfile:

gem 'cloudmersive-convert-api-client', '~> 1.3.3'

Then all we need to do is use the following code to call the convert_image_image_set_dpi function and set the desired new PPI/DPI:

# load the gem
require 'cloudmersive-convert-api-client'
# setup authorization
CloudmersiveConvertApiClient.configure do |config|
# Configure API key authorization: Apikey
config.api_key['Apikey'] = 'YOUR API KEY'
# Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
#config.api_key_prefix['Apikey'] = 'Bearer'
end
api_instance = CloudmersiveConvertApiClient::ConvertImageApi.newdpi = 56 # Integer | New DPI in pixels-per-inch, for example 300 DPI or 600 DPIinput_file = File.new("/path/to/file") # File | Input file to perform the operation on.begin
#Change image DPI
result = api_instance.convert_image_image_set_dpi(dpi, input_file)
p result
rescue CloudmersiveConvertApiClient::ApiError => e
puts "Exception when calling ConvertImageApi->convert_image_image_set_dpi: #{e}"
end

And voila! Our image is now the desired DPI/PPI. This can be extremely useful when moving between web use and printing for example. Our convert and image processing APIs have a great deal of other functionality related to photos and their formatting, so be sure to take a look at what else they can do.

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