catdad-experiments / heic-convert

🤳 convert heic/heif images to jpeg and png
245 stars 24 forks source link

TypeError: input buffer is not a HEIC image #22

Closed zeke closed 2 years ago

zeke commented 2 years ago

Hi @catdad 👋🏼 🐈‍⬛

Human-dad here. Thanks for writing these modules. 🙏🏼

I"m trying to use heic-convert to convert a bunch of files exported from Google Photos with Google Takeout, but getting an error input buffer is not a HEIC image.

I created a repro repo here: https://github.com/zeke/heic-convert-test

Am I missing something obvious?


$ file flower.heic 

flower.heic: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, Exif Standard: [TIFF image data, big-endian, direntries=10, manufacturer=Apple, model=iPhone SE (2nd generation), orientation=upper-right, xresolution=167, yresolution=175, resolutionunit=2, software=15.1, datetime=2022:01:13 13:23:04, hostcomputer=iPhone SE (2nd generation)TIFF image data, big-endian, direntries=10, manufacturer=Apple, model=iPhone SE (2nd generation), orientation=upper-right, xresolution=167, yresolution=175, resolutionunit=2, software=15.1, datetime=2022:01:13 13:23:04, hostcomputer=iPhone SE (2nd generation)], baseline, precision 8, 4032x3024, components 3
{
  inputBuffer: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff e1 17 0a 45 78 69 66 00 00 4d 4d 00 2a 00 00 00 08 00 0a 01 0f 00 02 00 00 00 06 00 00 ... 2150157 more bytes>
}
TypeError: input buffer is not a HEIC image
    at decodeBuffer (/Users/z/Desktop/heic-convert-test/node_modules/heic-decode/index.js:48:11)
    at module.exports (/Users/z/Desktop/heic-convert-test/node_modules/heic-decode/index.js:69:46)
    at convert (/Users/z/Desktop/heic-convert-test/node_modules/heic-convert/index.js:39:25)
    at module.exports (/Users/z/Desktop/heic-convert-test/node_modules/heic-convert/index.js:56:70)
    at /Users/z/Desktop/heic-convert-test/index.js:9:30
zeke commented 2 years ago

For future travelers, I found another way with ImageMagick:

find . -name '*.HEIC' -exec mogrify -format jpg {} +
catdad commented 2 years ago

Usually, you get that error when the image is not a HEIC. file seems to tell you it's a jpeg. Are you sure it is not a jpeg that has the wrong extension? That would not surprise me with Google takeout

catdad commented 2 years ago

Okay, I can confirm that the flower.heic file uploaded to your repro repo in is fact a jpeg image. It just has the wrong format. You can find a good amount of folks complaining about this actually -- Google Photos will compress images uploaded to it (unless you specifically select "original quality" from the settings). When it does this compression, it converts the images to jpegs but still keeps the .heic file extension on them, which just leads to a lot of confusion.

ImageMagick handles this as it will determine which codec to use to load the image entirely on its own. So with your command, it will see that the image being loaded is a jpeg, load it as a jpeg, and then re-encode it back to a jpeg to write the output. With your particular files (at least judging by flower.heic which was provided as a sample, that's actually a bad idea. Re-encoding jpeg images leads to introducing more and more artifacts every time the image is encoded, leading to worse quality every time. The image you provided it already compressed to 86% of original quality. I would recommend that you do not re-encode these files. Simply rename them to a .jpg file extension.

zeke commented 2 years ago

@catdad! Thank you for the thorough response! Very helpful.

I am now meagerly sponsoring you! In three to six months I will have bought you a coffee. 🙏🏼