ApeironTsuka / node-webpmux

A mostly 1:1 re-implementation of webpmux as a Node module in pure Javascript. Only thing currently missing is a command-line version.
GNU Lesser General Public License v3.0
26 stars 8 forks source link

Set exif data #1

Closed mpirescarvalho closed 3 years ago

mpirescarvalho commented 3 years ago

It's possible to set exif data for NOT ANIMATED webp files? I see the exif property but I'm not sure how could I save it back to a file

ApeironTsuka commented 3 years ago

It should be possible, but the .exif property is the raw, already-encoded EXIF data as it's found in the WEBP itself. It was outside the scope at the time to actually parse the EXIF chunk, and it's mostly kept (along with the XMP and ICC chunks) so that they don't get stripped out when re-saving WEBPs that have them defined.

mpirescarvalho commented 3 years ago

I have a .webp and a .exif file

using webpmux binary I could do: webpmux -set exif image_metadata.exif in.webp -o exif_container.webp

would that be possible with this library?

ApeironTsuka commented 3 years ago

At the moment, no. There's currently no way to save non-animated WEBPs.

I'll see about adding the ability to save non-animated WEBPs later today. I should have time. This library was originally intended mostly for reading metadata (as I couldn't find anything out there, not even the webpmux binary, that would give me access to the level of detail I needed at the time about stored animations). However, I do see the benefit to having the ability to set EXIF/ICC/XMP like this and feel a little silly for not already implementing it properly.

mpirescarvalho commented 3 years ago

And just to mention:

https://github.com/ApeironTsuka/node-webpmux/blob/402ef27cfae7fef506f2b1131a124af8d74eaa5f/webp.mjs#L120-L121

I think this sould be:

...createBasicChunk('EXIF', this.data.exif.raw)

instead of:

...createBasicChunk('EXIF', this.data.extended.exif.raw)
ApeironTsuka commented 3 years ago

Just pushed an update containing a .save and a static Image.save. Also fixed the EXIF/XMP/ICC chunk saving, as you're right, it should've been this.data.exif.raw. Feel free to re-open this if the save function doesn't work as expected.

mpirescarvalho commented 3 years ago

I'm sorry, but there's still something wrong, and I couldn't figure out if it is on my end.

Take a look at these two images (github doesn't support webp, so I'm sending prints):

I used webpmux binary to set the exif data to this webp file, and it opens as expected: image

This other one I used your library to set the exif data, but it broke the image: image

Now, looking at the images as text, I can see that the webpmux binary puts the exif data at the end: image

While your library puts it at the top: image

Maybe this is causing the images to be broken?

This is how I'm setting the data:

let img = new NodeWebP.Image();
await img.load(resultPath);
img.exif = exif
await img.save(resultPath);

It's the same data for both images

ApeironTsuka commented 3 years ago

Sorry about that. Never write code when half asleep and forget to test afterwards. It should hopefully work now.

mpirescarvalho commented 3 years ago

It didn't work 🙁

Is there something I can do to help?

ApeironTsuka commented 3 years ago

I'm not really sure. From my (admittedly limited) testing using GIMP to write the metadata, it works as expected. Both extracting the EXIF chunk to file via the webpmux binary and this library, as well as setting the EXIF chunk produce identical, working images. Does it work when using the webpmux binary set the EXIF data? If it does, could you upload the images/EXIF file somewhere and I can take a look?

mpirescarvalho commented 3 years ago

Does it work when using the webpmux binary set the EXIF data?

Yes, it works.

I've upload the files to this repo

Files:

Note that the webpmux bin generated one opens normally, while the node-webpmux generated do not

ApeironTsuka commented 3 years ago

Thanks. Should be definitely fixed now. Tested by loading webpmux-bin-image.webp and saving it. Output is identical, and displays fine in Chrome. Turns out the order the chunks are saved matters more than I thought. I don't recall the documentation mentioning that the ALPH chunk must come before the VP8 chunk or it's not a valid image.

mpirescarvalho commented 3 years ago

It worked, many thanks!