Converting TIFFS to a web friendly image type in Node (PNG, JPG etc)

Posted by

I’ve recently updated TIFF to PNG to use the new tech available in the newer versions of node. You can see it here on Github.


Before we get into the example Javascript, you will need to install ImageMagick. This setup will vary depending on your operating system, for more information see the ImageMagick downloads page. I personally use HomeBrew (MacOSX & you’ll need an up to date version of Xcode command line tools):

brew install imagemagick


This is a short example of how to use TIFF to PNG. Lets say you have an array of paths to TIFF files that you need to convert to a usable web format, lets use PNG’s for this example.

Assuming you have a version of node installed that supports async/await you’ll need to install the plugin:

npm install tiff-to-png


yarn add tiff-to-png

below is a block of code that will process the conversion of 2 TIFF files, it will then retrieve the paths of the outputted files and return them for whatever purpose you require.

const TIFFConverter = require('tiff-to-png'),
  fs = require('fs'),
  util = require('util'),
  path = require('path');

// Set a few options
const options = {
  type: 'png',
  scene: 1,
  tmpPath: path.join(__dirname, 'tmp'),
  autoRemoveTmp: true

const converter = new TIFFConverter(options);

 * Converts an array of TIFFs and places them in public/tiffs
 * @param {array} paths - an array of path strings
 * @returns {promise} resolves with a readable array of output paths
async function convertTIFFs(paths) {
  const location = path.join(__dirname, 'public/tiffs')
  const { converted, errors } = await converter.convertArray(paths, location);

  // Get all the file paths
  const readdir = util.promisify(fs.readdir);
  const files = await Promise.all(converted.filter(c => !!c.success)
    .map(async ({ target, tiff }) => {
      const filenames = await readdir(target);
      const files = => target + f);
      return { files, original: tiff };

  return { files, errors };

async function start() {
  // This is hardcoded, but could be pulled from anywhere:
  // database, post data (form) etc
  var myTIFFs = [
    path.join(__dirname, 'tiff_samples/sample1.tif'),
    path.join(__dirname, 'tiff_samples/sample2.tif')

  const { files, errors } = await convertTIFFs(myTIFFs);

  // Do whatever you need to do with the file paths

  // Do whatever you need to do with the errors

When ‘start()’ is called the script will do the following:

  1. Pass the 2 hardcoded tiff paths I have added into TIFF to PNG
  2. TIFF to PNG will process the files and put the PNGs in the ‘/public/tiffs’
    folder, each tiff with it’s own folder
  3. The results from TIFF to PNG will be filtered to remove the unsuccessful
    conversions, then passed to ‘fs.readdir’ which will read the directory and
    return the file paths of all contents.
  4. The results will then be passed back accompanied by the list of
    errors to the ‘start’ function to be handled in whatever way required
    (I’ve just logged them for this example)


A few things to keep in mind:

  • The path you wish to save the conversion in needs to exist before
    TIFF to PNG will work
  • Paths passed into TIFF to PNG should be absolute, blame ImageMagick.
    You can use ‘~’ but in this example fs.readdir won’t work.
  • If you want to see how the conversion is going just add ‘logLevel: 1’
    to the options.
  • If you don’t pass a temporary path in (used by ImageMagick) it will
    use whatever the system default is. Also it’s a good idea to autoRemoveTmp
    or you could end up with a lot of files gathering up never to be used again.
    By default it is set to false.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s