How to Change the DPI of an Image in Node.js

Cloudmersive
2 min readDec 7, 2023

--

Resizing an image’s DPI is a reliable way to impact its file size without changing its height and width proportions.

Using the below code, we can take advantage of API that allows us to customize the DPI (dots per inch) of an image by entering an integer in our request. We can use this API for free with a free-tier API key, which will allow up to 800 API calls per month with no additional commitment.

To get started, we can install the SDK — either using NPM install:

npm install cloudmersive-convert-api-client --save

Or by adding this snippet to our package.json:

  "dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}

Finally, we can use the below code to call the function & customize our request:

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;

// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';



var apiInstance = new CloudmersiveConvertApiClient.ConvertImageApi();

var dpi = 56; // Number | New DPI in pixels-per-inch, for example 300 DPI or 600 DPI

var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.


var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.convertImageImageSetDPI(dpi, inputFile, callback);

Now we can easily change the file size of an image. Note that fewer dots per inch will impact the quality of the image as well.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet