How to Improve Nodejs Image Processing Pipelines using Free APIs

Cloudmersive
4 min readMay 22, 2024

We don’t have to write a ton of new code to expand the capabilities of our Nodejs image processing pipelines.

Instead, we can easily take advantage of several free image processing APIs to automate conversions between myriad image formats, change the Dots per Inch (DPI) of our images, and retrieve detailed information about our images in a neatly organized response object.

We can structure our API calls using the complementary, ready-to-run code examples included further down the page, and we can authorize our API calls with a single (free) API key. This will allow us to make up to 800 API calls per month with zero commitments (our call total will simply reset the following month once we reach it).

First, we need to install the client SDK. We can either run the following NPM command:

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

Or we can add the Node client to our package.json:

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

Now we can begin calling various functions to perform actions on our image files. In each of the code blocks below, we can replace the ‘YOUR API KEY’ placeholder text with our own API key string to authorize our API calls.

Using the following code, we can convert between a huge variety of image formats — including common formats like PNG, JPG, and WEBP and less common formats like WDP, TGA, and VIFF. In var format1 and var format2, we can replace the format1_example and format2_example placeholder strings with our input and output image format extensions:

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 format1 = "format1_example"; // String | Input file format as a 3+ letter file extension. You can also provide UNKNOWN for unknown file formats. Supported formats include AAI, ART, ARW, AVS, BPG, BMP, BMP2, BMP3, BRF, CALS, CGM, CIN, CMYK, CMYKA, CR2, CRW, CUR, CUT, DCM, DCR, DCX, DDS, DIB, DJVU, DNG, DOT, DPX, EMF, EPDF, EPI, EPS, EPS2, EPS3, EPSF, EPSI, EPT, EXR, FAX, FIG, FITS, FPX, GIF, GPLT, GRAY, HDR, HEIC, HPGL, HRZ, ICO, ISOBRL, ISBRL6, JBIG, JNG, JP2, JPT, J2C, J2K, JPEG/JPG, JXR, MAT, MONO, MNG, M2V, MRW, MTV, NEF, ORF, OTB, P7, PALM, PAM, PBM, PCD, PCDS, PCL, PCX, PDF, PEF, PES, PFA, PFB, PFM, PGM, PICON, PICT, PIX, PNG, PNG8, PNG00, PNG24, PNG32, PNG48, PNG64, PNM, PPM, PSB, PSD, PTIF, PWB, RAD, RAF, RGB, RGBA, RGF, RLA, RLE, SCT, SFW, SGI, SID, SUN, SVG, TGA, TIFF, TIM, UIL, VIFF, VICAR, VBMP, WDP, WEBP, WPG, X, XBM, XCF, XPM, XWD, X3F, YCbCr, YCbCrA, YUV

var format2 = "format2_example"; // String | Output (convert to this format) file format as a 3+ letter file extension. Supported formats include AAI, ART, ARW, AVS, BPG, BMP, BMP2, BMP3, BRF, CALS, CGM, CIN, CMYK, CMYKA, CR2, CRW, CUR, CUT, DCM, DCR, DCX, DDS, DIB, DJVU, DNG, DOT, DPX, EMF, EPDF, EPI, EPS, EPS2, EPS3, EPSF, EPSI, EPT, EXR, FAX, FIG, FITS, FPX, GIF, GPLT, GRAY, HDR, HEIC, HPGL, HRZ, ICO, ISOBRL, ISBRL6, JBIG, JNG, JP2, JPT, J2C, J2K, JPEG/JPG, JXR, MAT, MONO, MNG, M2V, MRW, MTV, NEF, ORF, OTB, P7, PALM, PAM, PBM, PCD, PCDS, PCL, PCX, PDF, PEF, PES, PFA, PFB, PFM, PGM, PICON, PICT, PIX, PNG, PNG8, PNG00, PNG24, PNG32, PNG48, PNG64, PNM, PPM, PSB, PSD, PTIF, PWB, RAD, RAF, RGB, RGBA, RGF, RLA, RLE, SCT, SFW, SGI, SID, SUN, SVG, TGA, TIFF, TIM, UIL, VIFF, VICAR, VBMP, WDP, WEBP, WPG, X, XBM, XCF, XPM, XWD, X3F, YCbCr, YCbCrA, YUV

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.convertImageImageFormatConvert(format1, format2, inputFile, callback);

Next, using the below code, we can enter a custom integer to set the DPI of our images. This primarily impacts the resolution of images we’re planning to print:

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);

Finally, we can use the below code to gather detailed information about an image, including its size, format and MIME type, compression, and EXIF data (e.g., location, DPI, unique colors, transparency information, etc.):

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 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.convertImageGetImageInfo(inputFile, callback);

Our image retrieval action will return information structured like the following JSON response model:

{
"Successful": true,
"ColorSpace": "string",
"ColorType": "string",
"Width": 0,
"Height": 0,
"CompressionLevel": 0,
"ImageHashSignature": "string",
"HasTransparency": true,
"MimeType": "string",
"ImageFormat": "string",
"DPIUnit": "string",
"DPI": 0,
"ColorCount": 0,
"BitDepth": 0,
"Comment": "string",
"ExifProfileName": "string",
"ExifValues": [
{
"Tag": "string",
"DataType": "string",
"DataValue": "string"
}
]
}

That’s all the code we’ll need! Now we have several free & easy-to-use solutions for our Nodejs image processing pipelines.

--

--

Cloudmersive

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