A very complete, highly tested, standards-driven (but customizable) EXIF reader/writer lovingly written in Go.
 
 
 
Go to file
Dustin Oprea d3b45fae77 We now have to pass a specific tag-index in from the top.
- This will allow us to handle custom tags and, with a little more work,
  custom IFDs.
2018-07-26 22:45:43 -04:00
assets assets: Attached PNG standards documents to project. 2018-06-18 00:43:27 -04:00
exif-read-tool We now have to pass a specific tag-index in from the top. 2018-07-26 22:45:43 -04:00
.travis.yml ci: Quoted (and corrected) interpretation of Go versions. 2018-06-13 01:22:21 -04:00
README.md exif: Deimplemented IsExif(). 2018-06-06 04:37:43 -04:00
TODO tags_unknown: Seeded a first test for encode/decode. 2018-05-02 06:04:14 -04:00
TODO-WRITES Tweaked write to-dos. 2018-05-01 10:20:39 -04:00
common_test.go Reorganized to not require GOPATH unless testing. 2018-06-16 09:49:14 -04:00
error.go ifd_enumerate: Added find-by-id and find-by-name to tags in `ifd`. 2018-05-03 04:09:06 -04:00
exif.go We now have to pass a specific tag-index in from the top. 2018-07-26 22:45:43 -04:00
exif_test.go We now have to pass a specific tag-index in from the top. 2018-07-26 22:45:43 -04:00
gps.go gps: Bugfix: GpsInfo method receivers now references. 2018-06-09 11:29:16 -04:00
ifd.go ifd_builder: Added ChildWithIfdIdentity(). 2018-06-10 18:45:38 -04:00
ifd_builder.go We now have to pass a specific tag-index in from the top. 2018-07-26 22:45:43 -04:00
ifd_builder_encode.go ifd_builder_encode: Now allow the IB to be empty. 2018-06-13 06:16:09 -04:00
ifd_builder_encode_test.go We now have to pass a specific tag-index in from the top. 2018-07-26 22:45:43 -04:00
ifd_builder_test.go We now have to pass a specific tag-index in from the top. 2018-07-26 22:45:43 -04:00
ifd_enumerate.go We now have to pass a specific tag-index in from the top. 2018-07-26 22:45:43 -04:00
ifd_enumerate_test.go We now have to pass a specific tag-index in from the top. 2018-07-26 22:45:43 -04:00
ifd_tag_entry.go ifd_enumerate: Now parse the GPS info. 2018-06-09 02:11:24 -04:00
ifd_tag_entry_test.go ifd_tag_entry: Decoupled test from header semantics. 2018-06-06 05:26:13 -04:00
readme.go Added package description. 2018-05-01 11:04:55 -04:00
tags.go We now have to pass a specific tag-index in from the top. 2018-07-26 22:45:43 -04:00
tags_data.go tags: We now use a singleton TagIndex internally. 2018-06-07 05:15:51 -04:00
tags_test.go We now have to pass a specific tag-index in from the top. 2018-07-26 22:45:43 -04:00
tags_unknown.go ifd_enumerate: Implemented (*Ifd).ChildWithName() . 2018-06-07 21:36:14 -04:00
tags_unknown_test.go We now have to pass a specific tag-index in from the top. 2018-07-26 22:45:43 -04:00
type.go type: Fixed string format. 2018-06-13 01:19:28 -04:00
type_encode.go type_encode: Added to-do. 2018-05-25 21:12:45 -04:00
type_encode_test.go ifd_builder: Implemented NewBuilderTagFromConfig() BT factory for testing. 2018-04-27 03:42:59 -04:00
type_test.go type: Fix for ASCII value encodings having an extra NUL. 2018-06-13 00:48:30 -04:00
utility.go ifd_builder: Added test for encoding a complete IFD. 2018-04-29 01:51:12 -04:00
utility_test.go utility: Added tests. 2018-05-04 05:09:27 -04:00

README.md

Build Status Coverage Status GoDoc

Overview

This package provides native Go functionality to parse EXIF information out of images.

NOTICE

This implementation is in active development. The reader functionality is reasonably complete and there are unit-tests covering the core functionality.

Remaining Tasks:

  • A couple of tags needing special handling still need to be implemented. Currently these will show "!DEFINED!" for their values. See here for more information
  • Creating/updating tags.

Getting

To get the project:

$ go get -t github.com/dsoprea/go-exif

Testing

The traditional method:

$ go test github.com/dsoprea/go-exif

Usage

Create an instance of the Exif type and call Scan() with a byte-slice, where the first byte is the beginning of the raw EXIF data. You may pass a callback that will be invoked for every tag or nil if you do not want one. If no callback is given, you are effectively just validating the structure or parsing of the image.

Obviously, it is most efficient to properly parse the media file and then provide the specific EXIF data to be parsed, but there is also a heuristic for finding the EXIF data within the media blob, directly. This means that, at least for testing or curiosity, you do not have to parse or even understand the format of image or audio file in order to find and decode the EXIF information inside of it. See the usage of the IsExif method in the example.

Reader Tool

There is a reader implementation included as a runnable tool:

$ go get github.com/dsoprea/go-exif/exif-read-tool
$ go build -o exif-read-tool github.com/dsoprea/go-exif/exif-read-tool
$ exif-read-tool -filepath "<media file-path>"

Example output:

IFD=[IFD] ID=(0x010f) NAME=[Make] COUNT=(6) TYPE=[ASCII] VALUE=[Canon]
IFD=[IFD] ID=(0x0110) NAME=[Model] COUNT=(22) TYPE=[ASCII] VALUE=[Canon EOS 5D Mark III]
IFD=[IFD] ID=(0x0112) NAME=[Orientation] COUNT=(1) TYPE=[SHORT] VALUE=[1]
IFD=[IFD] ID=(0x011a) NAME=[XResolution] COUNT=(1) TYPE=[RATIONAL] VALUE=[72/1]
IFD=[IFD] ID=(0x011b) NAME=[YResolution] COUNT=(1) TYPE=[RATIONAL] VALUE=[72/1]
IFD=[IFD] ID=(0x0128) NAME=[ResolutionUnit] COUNT=(1) TYPE=[SHORT] VALUE=[2]
IFD=[IFD] ID=(0x0132) NAME=[DateTime] COUNT=(20) TYPE=[ASCII] VALUE=[2017:12:02 08:18:50]
...

You can also print the raw, parsed data as JSON:

$ exif-read-tool -filepath "<media file-path>" -json

Example output:

[
    {
        "ifd_name": "IFD",
        "parent_ifd_name": "",
        "ifd_index": 0,
        "tag_id": 271,
        "tag_name": "Make",
        "tag_type_id": 2,
        "tag_type_name": "ASCII",
        "unit_count": 6,
        "value": "Canon",
        "value_string": "Canon"
    },
    {
        "ifd_name": "IFD",
        "parent_ifd_name": "",
        "ifd_index": 0,
        "tag_id": 272,
        "tag_name": "Model",
        "tag_type_id": 2,
        "tag_type_name": "ASCII",
        "unit_count": 22,
        "value": "Canon EOS 5D Mark III",
        "value_string": "Canon EOS 5D Mark III"
    },
...
    {
        "ifd_name": "Exif",
        "parent_ifd_name": "IFD",
        "ifd_index": 0,
        "tag_id": 37121,
        "tag_name": "ComponentsConfiguration",
        "tag_type_id": 7,
        "tag_type_name": "UNDEFINED",
        "unit_count": 4,
        "value": {
            "ConfigurationId": 2,
            "ConfigurationBytes": "AQIDAA=="
        },
        "value_string": "ComponentsConfiguration\u003cID=[YCBCR] BYTES=[1 2 3 0]\u003e"
    },
...
    {
        "ifd_name": "IFD",
        "parent_ifd_name": "",
        "ifd_index": 1,
        "tag_id": 514,
        "tag_name": "JPEGInterchangeFormatLength",
        "tag_type_id": 4,
        "tag_type_name": "LONG",
        "unit_count": 1,
        "value": "21491",
        "value_string": "21491"
    }
]

Example

f, err := os.Open(filepathArgument)
log.PanicIf(err)

data, err := ioutil.ReadAll(f)
log.PanicIf(err)

foundAt := -1
for i := 0; i < len(data); i++ {
    if e.IsExif(data[i:i + 6]) == true {
        foundAt = i
        break
    }
}

if foundAt == -1 {
    fmt.Printf("EXIF data not found.\n")
    os.Exit(-1)
}

// Run the parse.

ti := exif.NewTagIndex()
visitor := func(ii exif.IfdIdentity, ifdIndex int, tagId uint16, tagType exif.TagType, valueContext exif.ValueContext) (err error) {
    it, err := ti.Get(ii, tagId)
    if err != nil {
        if log.Is(err, exif.ErrTagNotFound) {
            fmt.Printf("WARNING: Unknown tag: [%s] (%04x)\n", indexedIfdName, tagId)
            return nil
        } else {
            panic(err)
        }
    }

    valueString := ""
    if tagType.Type() == exif.TypeUndefined {
        value, err := exif.UndefinedValue(indexedIfdName, tagId, valueContext, tagType.ByteOrder())
        if log.Is(err, exif.ErrUnhandledUnknownTypedTag) {
            valueString = "!UNDEFINED!"
        } else if err != nil {
            panic(err)
        } else {
            valueString = fmt.Sprintf("%v", value)
        }
    } else {
        valueString, err = tagType.ResolveAsString(valueContext, true)
        if err != nil {
            panic(err)
        }
    }

    fmt.Printf("IFD=[%s] ID=(0x%04x) NAME=[%s] COUNT=(%d) TYPE=[%s] VALUE=[%s]\n", ii.IfdName, tagId, it.Name, valueContext.UnitCount, tagType.Name(), valueString)
    return nil
}

err = Visit(data[foundAt:], visitor)
log.PanicIf(err)

Contributing

EXIF has an excellently-documented structure but there are a lot of devices and manufacturers out there. There are only so many files that we can personally find to test against, and most of these are images that have been generated only in the past few years. JPEG, being the largest implementor of EXIF, has been around for even longer (but not much). Therefore, there is a lot of different kinds of compatibility to test for.

If you are able to help, it would be deeply appreciated if you could run the included reader-tool against all of the EXIF-compatible files you have. This is mostly going to be JPEG files (but not all variations). If you are able to test a large number of files (thousands or millions), please post an issue no matter what. Mention how many files you tried, whether there were any failures, and, if you would be willing, give us access to the failed files.

If you are able to test 1M+ files, I will give you credit on the project. The further back in time your images reach, the higher in the list your name/company will go.