How to rotate an image a set number of degrees in Node.JS

Today we are going to look at how to rotate an image by a specified number of degrees. This is going to be easy.

Use npm install to set up our API client.

npm install cloudmersive-image-api-client --save

Then invoke our function with this snippet of code.

var CloudmersiveImageApiClient = require('cloudmersive-image-api-client');var defaultClient = CloudmersiveImageApiClient.ApiClient.instance;// Configure API key authorization: Apikeyvar Apikey = defaultClient.authentications['Apikey'];Apikey.apiKey = 'YOUR API KEY';// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.apiKeyPrefix = 'Token';var apiInstance = new CloudmersiveImageApiClient.EditApi();var degrees = 1.2; // Number | Degrees to rotate the image; values range from 0.0 to 360.0.var imageFile = "/path/to/file"; // File | Image file to perform the operation on.  Common file formats such as PNG, JPEG are supported.var callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.editRotate(degrees, imageFile, callback);

Enter desired number of degrees, input your image, and you are done! This function can be used in conjunction with another of our functions that detects a document image’s rotation, allowing you to straighten it. This works great when preprocessing for OCR.

Image for post

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