[Golang] Read JPEG Image Date via Exif Metadata


Read JPEG image date from Exif metadata in Golang.

We use goexif package to read Exif metadata. Install it by:

$ go get -u github.com/rwcarlsen/goexif/exif

Source code:

date.go | repository | view raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package jpgdate

import (
	"fmt"
	"github.com/rwcarlsen/goexif/exif"
	"io/ioutil"
	"os"
	"path"
)

func ReadJpegDate(dirname string) {
	files, err := ioutil.ReadDir(dirname)
	if err != nil {
		panic(err)
	}

	for _, file := range files {
		fpath := path.Join(dirname, file.Name())
		println(fpath)
		f, err := os.Open(fpath)
		if err != nil {
			panic(err)
		}
		defer f.Close()
		x, err := exif.Decode(f)
		if err != nil {
			panic(err)
		}
		tm, _ := x.DateTime()
		fmt.Println(tm.Date())
	}
}
date_test.go | repository | view raw
1
2
3
4
5
6
7
package jpgdate

import "testing"

func TestReadJpegDate(t *testing.T) {
	ReadJpegDate("/home/foo/Desktop/Photos/")
}

Tested on: Ubuntu Linux 15.10, Go 1.6.


References:

[1]rwcarlsen/goexif: Decode embedded EXIF meta data from image files.
[2]
[3]golang read jpeg metadata
[4]python read jpeg metadata
[5]Reading video stream metadata with go : golang