catdad-experiments/heic-convert

🤳 convert heic/heif images to jpeg and png

JavaScript

322

68 commits

updated Dec 2, 2024

See the code

README

heic-convert

Convert HEIC/HEIF images to JPEG and PNG

ci npm-downloads npm-version

Install

npm install heic-convert

Usage

You can convert the first image in a HEIC, or convert all images in the file. In both cases, the options are the same.

options object

  • buffer - a Node Buffer or a Uint8Array - the HEIC image to convert
  • format - string - either JPEG or PNG
  • quality - number between 0 and 1, optional - the lossy compression quality to be used when converting to jpeg.

Usage in NodeJS

Convert the main image in a HEIC to JPEG

const { promisify } = require('util');
const fs = require('fs');
const convert = require('heic-convert');

(async () => {
  const inputBuffer = await promisify(fs.readFile)('/path/to/my/image.heic');
  const outputBuffer = await convert({
    buffer: inputBuffer,
    format: 'JPEG',
    quality: 1
  });

  await promisify(fs.writeFile)('./result.jpg', outputBuffer);
})();

Convert the main image in a HEIC to PNG

const { promisify } = require('util');
const fs = require('fs');
const convert = require('heic-convert');

(async () => {
  const inputBuffer = await promisify(fs.readFile)('/path/to/my/image.heic');
  const outputBuffer = await convert({
    buffer: inputBuffer,
    format: 'PNG'
  });

  await promisify(fs.writeFile)('./result.png', outputBuffer);
})();

Convert all images in a HEIC

const { promisify } = require('util');
const fs = require('fs');
const convert = require('heic-convert');

(async () => {
  const inputBuffer = await promisify(fs.readFile)('/path/to/my/image.heic');
  const images = await convert.all({
    buffer: inputBuffer,
    format: 'JPEG'
  });

  for (let idx in images) {
    const image = images[idx];
    const outputBuffer = await image.convert();
    await promisify(fs.writeFile)(`./result-${idx}.jpg`, outputBuffer);
  }
})();

The work to convert an image is done when calling image.convert(), so if you only need one of the images in a multi-image file, you can convert just that one from the images array and skip doing any work for the remaining images.

Note that while the converter returns a Promise and is overall asynchronous, a lot of work is still done synchronously, so you should consider using a worker thread in order to not block the main thread in highly concurrent production environments.

Usage in the browser

While the NodeJS version of heic-convert may be compiled for use in the browser with something like webpack, not all build tools necessarily like to compile all modules well. However, what further complicates things is that this module uses pure-javascript implementations of a jpeg and png encoder. But the browser has its own native encoders! Let's just use those instead of including a ton of extra code in your bundle.

When compiling a client-side project, use:

const convert = require('heic-convert/browser');

This is currently only supported in the main thread. Support for workers may be added in the future, but if you need it sooner, please create an issue or even a PR!

  • heic-cli - convert heic/heif images to jpeg or png from the command line
  • heic-decode - decode heic images to raw image data
  • libheif-js - libheif as a pure-javascript npm module
convert
heic
heic-to-jpg
heic-to-png
heif
image
jpg
npm-module
png

catdad-experiments/heic-convert

🤳 convert heic/heif images to jpeg and png

JavaScript

322

68 commits

updated Dec 2, 2024

See the code

README

heic-convert

Convert HEIC/HEIF images to JPEG and PNG

ci npm-downloads npm-version

Install

npm install heic-convert

Usage

You can convert the first image in a HEIC, or convert all images in the file. In both cases, the options are the same.

options object

  • buffer - a Node Buffer or a Uint8Array - the HEIC image to convert
  • format - string - either JPEG or PNG
  • quality - number between 0 and 1, optional - the lossy compression quality to be used when converting to jpeg.

Usage in NodeJS

Convert the main image in a HEIC to JPEG

const { promisify } = require('util');
const fs = require('fs');
const convert = require('heic-convert');

(async () => {
  const inputBuffer = await promisify(fs.readFile)('/path/to/my/image.heic');
  const outputBuffer = await convert({
    buffer: inputBuffer,
    format: 'JPEG',
    quality: 1
  });

  await promisify(fs.writeFile)('./result.jpg', outputBuffer);
})();

Convert the main image in a HEIC to PNG

const { promisify } = require('util');
const fs = require('fs');
const convert = require('heic-convert');

(async () => {
  const inputBuffer = await promisify(fs.readFile)('/path/to/my/image.heic');
  const outputBuffer = await convert({
    buffer: inputBuffer,
    format: 'PNG'
  });

  await promisify(fs.writeFile)('./result.png', outputBuffer);
})();

Convert all images in a HEIC

const { promisify } = require('util');
const fs = require('fs');
const convert = require('heic-convert');

(async () => {
  const inputBuffer = await promisify(fs.readFile)('/path/to/my/image.heic');
  const images = await convert.all({
    buffer: inputBuffer,
    format: 'JPEG'
  });

  for (let idx in images) {
    const image = images[idx];
    const outputBuffer = await image.convert();
    await promisify(fs.writeFile)(`./result-${idx}.jpg`, outputBuffer);
  }
})();

The work to convert an image is done when calling image.convert(), so if you only need one of the images in a multi-image file, you can convert just that one from the images array and skip doing any work for the remaining images.

Note that while the converter returns a Promise and is overall asynchronous, a lot of work is still done synchronously, so you should consider using a worker thread in order to not block the main thread in highly concurrent production environments.

Usage in the browser

While the NodeJS version of heic-convert may be compiled for use in the browser with something like webpack, not all build tools necessarily like to compile all modules well. However, what further complicates things is that this module uses pure-javascript implementations of a jpeg and png encoder. But the browser has its own native encoders! Let's just use those instead of including a ton of extra code in your bundle.

When compiling a client-side project, use:

const convert = require('heic-convert/browser');

This is currently only supported in the main thread. Support for workers may be added in the future, but if you need it sooner, please create an issue or even a PR!

  • heic-cli - convert heic/heif images to jpeg or png from the command line
  • heic-decode - decode heic images to raw image data
  • libheif-js - libheif as a pure-javascript npm module
convert
heic
heic-to-jpg
heic-to-png
heif
image
jpg
npm-module
png

Project health

Overall

2.4/10

Maintained

0/10

Code review

0/10
OpenSSF Scorecard

Latest release

2.1.0 · Nov 30, 2023

Releases this year

0

Significant stargazers

Wojciech Maj

3,571 followers · starred Apr 2024

xocialize

26 followers · starred May 2020

Magnus

34 followers · starred Dec 2025

Kumar Deepanshu

55 followers · starred Feb 2025