trimmer-io / go-xmp

A native Go SDK for the Extensible Metadata Platform (XMP)
Apache License 2.0
53 stars 11 forks source link

how can i use this with png files? #1

Open s3rj1k opened 6 years ago

s3rj1k commented 6 years ago

how can i use this with png files, creating and reading?

echa commented 6 years ago

There's no container format read/write support in go-xmp. You may have to write your own or use an existing parser to read/write native PNG or EXIF metadata. go-xmp helps you process the XMP data once you have it extracted.

If a binary file contains an XMP packet you can easily find and extract it using xmp.ScanPackets(io.Reader). See cmd/xmpinfo.go for an example. To embed an XMP packet into a binary file you would serialize the XMP tree using xmp.Marshal(d *xmp.Document) and write the []byte blob to your file following conventions to embed XMP into, say PNG, JPG, etc.

To read EXIF data into go-xmp you may use exiftool (it can output JSON) or goexif and insert all EXIF/TIFF key/value pairs into a xmp.Exif model in a loop:

import (
    "trimmer.io/go-xmp/exif"
    "trimmer.io/go-xmp/xmp"
)

func main() {
    // create an empty XMP document and insert an empty EXIF model
    d := xmp.NewDocument()
    e, _ := exif.MakeModel(d)

    // TODO: get the EXIF metadata as key/value list

    // then insert all EXIF fields as strings
    for _, tag := range exiftags {
        e.SetTag(tag.Key, tag.Value)
    }
}

Note that SetTag() is a convenience method and as such limited to strings for keys and values. Keys need to be the hex representation of the EXIF tag id, e.g. 0x0100 for image width, etc.

You can add other value types supported by XMP too, but you would have to set the fields in exif.ExifInfo directly, e.g. e.ImageWidth = exiftag.ImageWidth.

Arguably the custom data types in XMP are a major inconvenience and Go doesn't help much in mapping data types from external libs here (same with Adobe's XMP C++ SDK).

s3rj1k commented 6 years ago

are there any example code that actually embeds XMP to jpeg/png?

echa commented 6 years ago

No.