go-xmp icon indicating copy to clipboard operation
go-xmp copied to clipboard

how can i use this with png files?

Open s3rj1k opened this issue 6 years ago • 3 comments

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

s3rj1k avatar Mar 19 '18 15:03 s3rj1k

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).

echa avatar Mar 19 '18 16:03 echa

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

s3rj1k avatar Mar 21 '18 23:03 s3rj1k

No.

echa avatar Mar 22 '18 00:03 echa