goexif

repository·go1·Indexed 20 days ago

https://github.com/rwcarlsen/goexif

A Go library for decoding and inspecting EXIF metadata from image files, featuring support for TIFF tags and manufacturer-specific maker notes. It includes the `exif` and `tiff` packages, as well as the `exifstat` command-line utility for inspecting metadata and dumping JPEG thumbnails.

Tokens
1.1K
Snippets
4
Records
5
Agent score
22%

What's inside goexif

  1. How goexif packages are structured

    go1

    The functionality is split into two primary packages:

    1. exif: The main package for decoding EXIF data. It depends on the tiff package.
    2. tiff: Provides decoding for TIFF encoded data.

    Note: The project is currently in alpha and functionality is subject to change.

  2. Install goexif

    go1

    You can install the exif package (which includes the tiff dependency) or just the tiff package using go get.

    # To install the exif package (and its tiff dependency)
    go get github.com/rwcarlsen/goexif/exif
    
    # To install only the tiff package
    go get github.com/rwcarlsen/goexif/tiff
  3. Decode EXIF data from an image

    go1

    To extract EXIF metadata, use exif.Decode(f) where f is an io.Reader (such as an opened file).

    Key features:

    • Registering Parsers: You can optionally register camera maker note parsers (e.g., Nikon or Canon) using exif.RegisterParsers(mknote.All...) before decoding.
    • Retrieving Tags: Use x.Get(tag) to retrieve specific metadata tags.
    • Convenience Methods: The decoded object provides helper methods for common data like DateTime() and LatLong().
    package main
    
    import (
    	"fmt"
    	"log"
    	"os"
    
    	"github.com/rwcarlsen/goexif/exif"
    	"github.com/rwcarlsen/goexif/mknote"
    )
    
    func ExampleDecode() {
    	fname := "sample1.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)
    }
  4. Use the exifstat CLI tool

    go1

    The exifstat tool is a command-line utility for walking through and inspecting EXIF metadata in image files. It can output metadata as JSON strings for each field or dump the JPEG thumbnail of the first processed image to stdout.

    Flags

    • -mknote: Enables parsing of manufacturer-specific (makernote) data by registering all available mknote parsers.
    • -thumb: Dumps the JPEG thumbnail data of the first listed image file directly to stdout and then exits.

    Usage

    Pass one or more image filenames as arguments after the flags.

    # Inspect metadata for an image
    ./exifstat image.jpg
    
    # Inspect metadata including maker notes
    ./exifstat -mknote image.jpg
    
    # Extract the thumbnail to a file
    ./exifstat -thumb image.jpg > thumbnail.jpg