heic-convert vs heic2any
HEIC Image Conversion
heic-convertheic2any

HEIC Image Conversion

HEIC (High Efficiency Image Coding) is a modern image format that provides better compression than JPEG while maintaining high image quality. However, not all devices and applications support HEIC files, making it necessary to convert them to more widely accepted formats like JPEG or PNG. This is where HEIC conversion libraries come into play. They provide tools to programmatically convert HEIC images to other formats, enabling compatibility with a broader range of devices and software. This is particularly useful for web applications, image processing tools, and any platform that needs to handle images from devices like iPhones, which use HEIC as their default format.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
heic-convert03117.92 kB102 years agoISC
heic2any08252.72 MB243 years agoMIT

Feature Comparison: heic-convert vs heic2any

Conversion Formats

  • heic-convert:

    heic-convert primarily supports converting HEIC files to JPEG and PNG formats. It is focused on providing high-quality conversions with minimal configuration, making it easy to use for straightforward tasks.

  • heic2any:

    heic2any supports converting HEIC files to multiple formats, including JPEG, PNG, and WEBP. This flexibility allows developers to choose the output format based on their needs, making it a more versatile option for applications that require support for various image formats.

Output Quality Control

  • heic-convert:

    heic-convert provides basic quality control for JPEG conversions, allowing users to specify the quality level (0-100) for the output images. However, it is relatively simple and does not offer advanced features for fine-tuning the output quality.

  • heic2any:

    heic2any offers more comprehensive control over the output quality, especially for JPEG conversions. It allows developers to set quality parameters and provides better handling of different formats, making it a better choice for applications that require precise control over image quality.

Batch Processing

  • heic-convert:

    heic-convert supports batch processing of HEIC files, allowing users to convert multiple images in a single operation. This feature is particularly useful for users who need to convert large batches of images quickly and efficiently.

  • heic2any:

    heic2any also supports batch processing, enabling the conversion of multiple HEIC files at once. Its ability to handle multiple formats in a single batch makes it a versatile choice for applications that need to process images in bulk.

Ease of Use: Code Examples

  • heic-convert:

    heic-convert is known for its simplicity and ease of use. The API is straightforward, making it easy for developers to integrate into their applications quickly. Here’s a quick example:

    const heicConvert = require('heic-convert');
    
    async function convertHEIC() {
      const buffer = await fs.promises.readFile('image.heic');
      const outputBuffer = await heicConvert({
        buffer: buffer,
        format: 'JPEG', // Output format
        quality: 0.8, // Quality (0-1)
      });
    
      await fs.promises.writeFile('image.jpg', outputBuffer);
    }
    
    convertHEIC();
    
  • heic2any:

    heic2any provides a simple API for converting HEIC images, with good documentation that makes it easy to understand and use. Here’s an example of how to use it:

    import { heic2any } from 'heic2any';
    
    async function convertHEIC() {
      const file = document.getElementById('input').files[0];
      const outputBlob = await heic2any({
        blob: file,
        toType: 'image/png', // Output format
        quality: 0.8,
      });
    
      const url = URL.createObjectURL(outputBlob);
      document.getElementById('output').src = url;
    }
    
    convertHEIC();
    

How to Choose: heic-convert vs heic2any

  • heic-convert:

    Choose heic-convert if you need a simple, straightforward solution for converting HEIC images to JPEG or PNG with minimal setup. It is particularly useful for quick conversions and supports batch processing.

  • heic2any:

    Choose heic2any if you require a more versatile solution that supports converting HEIC to multiple formats (JPEG, PNG, WEBP) and offers better control over the output quality and format. It is ideal for applications that need more flexibility in handling different image formats.

README for heic-convert

heic-convert

Convert HEIC/HEIF images to JPEG and PNG

ci npm-downloads npm-version

Install

npm install heic-convert

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, // the HEIC file buffer
    format: 'JPEG',      // output format
    quality: 1           // the jpeg compression quality, between 0 and 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, // the HEIC file buffer
    format: 'PNG'        // output format
  });

  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, // the HEIC file buffer
    format: 'JPEG'       // output format
  });

  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!

Related

  • 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