Node.js将HEIC/HIF文件转换为jpeg(离线)

huangapple go评论63阅读模式
英文:

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

问题

我正在尝试找到一种将heic/hif文件在服务器端转换为jpeg的方法,以便它们可以更兼容其他工具/程序。似乎在Node.js中唯一找到的库是heic-convert(或一些相关的库,它们都存在相同的问题)。

当我直接从README中复制示例时,我正在处理的图像在转换后变成了完全绿色。

这是我的测试代码,主要来自README:

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);
}

这是我正在测试的原始图像(来自Sony a7 IV)。

但当我通过转换器运行它时,结果变成了这样

有关解决为什么这个库不起作用或其他可用库的帮助将会非常有用!

英文:

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!

答案1

得分: 0

我找到了一个目前足够使用的库,这是一个 Node.js 包 imagemagick

有点不太方便的是,这个包只是一个围绕该库的 Node.js 包装器,而不是一个独立的一体化包。因此,您首先需要从这里安装实际的库。

我认为这些 HIF 文件的问题可能是它们使用了比 heic-convert 能处理的 heic 标准更新版本。因为该包可以处理来自我的 iPhone 的 heic 文件,只是不能处理从我的 Sony a7 IV 创建的 HIF 文件。我不确定如何查看文件的 heic 版本,但我认为它们使用了不同的版本。幸运的是,imagemagick 库似乎支持两者。

英文:

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.

huangapple
  • 本文由 发表于 2023年6月22日 13:11:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528758.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定