node.jsimageconvertersjpegheic

Node.js convert HEIC/HIF file to jpeg (offline)


I am trying to find a way to convert heic/hif files to jpeg server side so that they can be more compatible with other tools/programs. Seems like the only library I can find for node.js is heic-convert (or a few related ones that give the same problem).

When I copy the example straight from the read me the image I am working with turns completely green after it is converted.

This is my test code, taken mostly from the read me.

import fs from 'fs';
import convert from 'heic-convert';

var filePath = '/path/to/my/image.HIF';

test();

async function test() {
    var inputBuffer = await fs.promises.readFile(filePath);
    var outputBuffer = await convert({
        buffer: inputBuffer, // the HEIC file buffer
        format: 'JPEG',      // output format
        quality: 1           // the jpeg compression quality, between 0 and 1
    });
    await fs.promises.writeFile(filePath.replace(/\.hif$/i, '.jpg'), outputBuffer);
}

This is the original image I am testing with (taken from a Sony a7 IV).

But when I run it through the converter, it comes out like this.

Any help with figuring out why this library isn't working or some other library that would work would be awesome!


Solution

  • I found a library that will work enough for now, the node package imagemagick.

    It's a little bit inconvenient because this package is just a node.js wrapper around the library and not an all in one self contained package. So you have to install the actual library first from here.

    I think the issue with these HIF files might have been that they use a newer version of the heic standard than the heic-convert can handle. Because that package does work with heic files taken from my iPhone, it just doesn't work with the HIF files created from my Sony a7 IV. I'm not sure how to see the heic version of the files, but I assume they're using different versions. Luckily, the imagemagick library seems to support both.