rwcarlsen / goexif

Decode embedded EXIF meta data from image files.
BSD 2-Clause "Simplified" License
627 stars 134 forks source link

ExifIFDPointer decode failed when attempting to read EXIF data. #74

Open FooSoft opened 5 years ago

FooSoft commented 5 years ago

Attempting to extract GPS metadata from an image known to contain it causes the following error to be logged:

loading EXIF sub-IFD: exif: sub-IFD ExifIFDPointer decode failed: zero length tag value

Go version:

go version go1.12.6 linux/amd64

Test code (basically copy-paste of sample):

package main

import (
    "fmt"
    "log"
    "os"

    "github.com/rwcarlsen/goexif/exif"
    "github.com/rwcarlsen/goexif/mknote"
)

func main() {
    fname := "osaka.jpg"

    f, err := os.Open(fname)
    if err != nil {
        log.Fatal(err)
    }

    // Optionally register camera makenote data parsing - currently Nikon and
    // Canon are supported.
    exif.RegisterParsers(mknote.All...)

    x, err := exif.Decode(f)
    if err != nil {
        log.Fatal(err)
    }

    camModel, _ := x.Get(exif.Model) // normally, don't ignore errors!
    fmt.Println(camModel.StringVal())

    focal, _ := x.Get(exif.FocalLength)
    numer, denom, _ := focal.Rat2(0) // retrieve first (only) rat. value
    fmt.Printf("%v/%v", numer, denom)

    // Two convenience functions exist for date/time taken and GPS coords:
    tm, _ := x.DateTime()
    fmt.Println("Taken: ", tm)

    lat, long, _ := x.LatLong()
    fmt.Println("lat, long: ", lat, ", ", long)
}

Image file: https://github.com/FooSoft/goldsmith-components/raw/master/plugins/exif/testdata/source/osaka.jpg

FooSoft commented 5 years ago

I should add that this was taken on a Huawei Mate SE, and all pictures it saves have this problem.

mrauh commented 5 years ago

Same here with pictures of a Huawei P20. Here is the output of a sample picture created with ExifTool: https://pastebin.com/bWF8Nus5

mrauh commented 5 years ago

I was able to solve my problem by ignoring non-critical decoding errors and using DateTime() instead of Get(exif.DateTimeOriginal).

@FooSoft As you are trying to extract GPS metadata, maybe the LatLong() function works for you.

Here is my snippet that finally works:

x, err := exif.Decode(f)
if err != nil {
    if exif.IsCriticalError(err) {
        return nil, err
    }
}
defer f.Close()

dt, err := x.DateTime()
if err != nil {
    return nil, err
}

// Use dt...
FooSoft commented 5 years ago

@mrauh thanks for the example, I did not realize IsCriticalError was something that I should be looking for; does not appear to be very intuitive.