6

Does anyone have an example on using "github.com/gohugoio/hugo/resources/images/exif" to extract metadata from a local image using Go?

I looked through the docs and since I'm new to Go I'm not 100% sure if I'm doing things write. I do read the image, but I'm not sure what the next step would be.

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

(Edit 1) Actually I think I found a solution:

d, err := exif.NewDecoder(exif.IncludeFields("File Type"))
x, err := d.Decode(f)
if err != nil {
  log.Fatal("Error: ", err)
}
fmt.Println(x)

however, the question is how do I know what fields are available? File Type for example returns <nil>

Tamas
  • 10,953
  • 13
  • 47
  • 77

1 Answers1

12

Looks like Hugo uses github.com/rwcarlsen/goexif.

The documentation of the package on go.dev shows Exif.Walk can walk the name and tag for every non-nil EXIF field.

Eg, a small program:

package main

import (
    "fmt"
    "log"
    "os"

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

type Printer struct{}

func (p Printer) Walk(name exif.FieldName, tag *tiff.Tag) error {
    fmt.Printf("%40s: %s\n", name, tag)
    return nil
}

func main() {
    if len(os.Args) < 2 {
        log.Fatal("please give filename as argument")
    }
    fname := os.Args[1]

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

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

    var p Printer
    x.Walk(p)
}

Example:

$ go run main.go IMG_123.JPG

                          ResolutionUnit: 2
                        YCbCrPositioning: 2
                                    Make: "Canon"
                                   Model: "Canon IXUS 255 HS"
              ThumbJPEGInterchangeFormat: 5620
                         PixelYDimension: 3000
                FocalPlaneResolutionUnit: 2
                            GPSVersionID: [2,3,0,0]
                             ExifVersion: "0230"
                            WhiteBalance: 1
                                DateTime: "2016:10:04 17:27:56"
                  CompressedBitsPerPixel: "5/1"
                                   ... etc ...
                             Orientation: 1
                            MeteringMode: 5
                             FocalLength: "4300/1000"
                         PixelXDimension: 4000
              InteroperabilityIFDPointer: 4982
                   FocalPlaneXResolution: "4000000/244"
                             XResolution: "180/1"
                 ComponentsConfiguration: ""
                       ShutterSpeedValue: "96/32"
                           ApertureValue: "101/32"
                       ExposureBiasValue: "-1/3"
                   FocalPlaneYResolution: "3000000/183"
                        SceneCaptureType: 0
Mark
  • 6,731
  • 1
  • 40
  • 38
  • Thanks Mark. Unfortunately this returns "2020/03/03 08:42:10 EOF exit status 1". (I am running this against `go version go1.14 darwin/amd64`) – Tamas Mar 03 '20 at 07:42
  • @Tamas go version looks ok. The EOF is reported from `Open` or `Decode`. Maybe the image has bad or missing EXIF data? Try different images – Mark Mar 03 '20 at 10:05