Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how can i use this with png files? #1

Open
s3rj1k opened this issue Mar 19, 2018 · 3 comments
Open

how can i use this with png files? #1

s3rj1k opened this issue Mar 19, 2018 · 3 comments

Comments

@s3rj1k
Copy link

s3rj1k commented Mar 19, 2018

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

@echa
Copy link
Collaborator

echa commented Mar 19, 2018

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
Copy link
Author

s3rj1k commented Mar 21, 2018

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

@echa
Copy link
Collaborator

echa commented Mar 22, 2018

No.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants