Using itk.js in a Node.js application

An itk-convert command line interface (CLI) example demonstrates how to use itk.js in a Node.js application. Find the full example in the itk-js/examples/Node.js directory of the GitHub repository.

This example assumes you are creating a Node.js package. If you do not already have a package.json file, create one, first.

Add itk to your project’s dependencies:

npm install --save itk

This adds itk to the dependencies section of your package.json file:

{
"name": "itk-convert",
"version": "1.1.0",
"description": "Convert images files from one format to another.",
[...]
"dependencies": {
"commander": "^2.14.1",
"itk": "^9.2.0"
}
}

Next, call functions like itk/readImageLocalFileSync or itk/writeImageLocalFileSync.

For example,

[...]

const inputFile = program.args[0]
const outputFile = program.args[1]

try {
const image = readImageLocalFileSync(inputFile);
const useCompression = true;
writeImageLocalFileSync(useCompression, image, outputFile);
} catch (error) {
console.error('Error during conversion:\n');
console.error(error);
}