image-js / fast-png

PNG image decoder and encoder written entirely in JavaScript
MIT License
334 stars 19 forks source link

Add the possibility to create PNG with meta information #26

Closed lpatiny closed 8 months ago

lpatiny commented 2 years ago

When encoding an image we would need to be able to add textual information following the specifications:

https://www.w3.org/TR/PNG/#11textinfo

We should also ensure that decoding can read those information.

targos commented 8 months ago

Do you have an example of file with such textual information so we can understand:

lpatiny commented 8 months ago

I guess one idea is that when you copy a molecule as PNG we can insert the smiles or molfile in it so that if we somehow copy it back from another software we cane extract the chemical structure.

Using imagemagick we can add a new keyword in a PNG using:

convert butane.png -set 'Smiles' 'CCCC' butane_meta.png

We can check if the keyword has been added using identify --verbose butane_meta.png

You will find here both images before and after adding the Smiles

Archive.zip

There are 3 possible chunks:

stropitek commented 8 months ago

FWIW excalidraw exports a png with a json in the metadata so that it can be re-edited:

CleanShot 2024-02-13 at 10 24 31

This is the png with the embedded data: excalidraw_export_embed

targos commented 8 months ago

Both your examples use the tEXt chunk, so we only need to implement the encoder part of it.

BTW, thanks @stropitek for your example, it made me realize that the decoding was wrong. Fixed in https://github.com/image-js/fast-png/pull/39

targos commented 8 months ago

@lpatiny is latin1 enough for your use case?

lpatiny commented 8 months ago

Yes latin1 is enough for SMILES and molfile.