kolesa-team / goexiv

Go bindings for the exiv2 library. Allows reading and writing EXIF/IPTC metadata to/from JPG, WEBP and PNG images.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build Status

Go bindings for exiv2 (http://www.exiv2.org)

The library allows reading and writing EXIF and IPTC metadata to/from JPG, WEBP, and PNG images.

It is based on https://github.com/abustany/goexiv and https://github.com/gitschneider/goexiv with support added for writing the metadata and various bugfixes.

Библиотека для записи и чтения метаданных EXIF и IPTC в изображениях формата JPG, WEBP и PNG. Основана на https://github.com/abustany/goexiv и https://github.com/gitschneider/goexiv с добавленной фукнциональностью для записи метаданных и исправлением ошибок.

Requirements

A libexiv2 library v0.27 is required (this might also work with newer versions, but it hasn't been tested).

On Ubuntu, libexiv2 can be installed from the package manager (sudo apt install libexiv2-dev), but there is no guarantee it comes with the version needed. So it is safer to install it manually:

  • Download and unpack the library from https://github.com/Exiv2/exiv2/releases/tag/v0.27.2
  • Install the library (the steps are taken from libexiv2 README):
    mkdir build && cd build
    cmake .. -DCMAKE_BUILD_TYPE=Release
    cmake --build .
    sudo make install
    sudo ldconfig
    
  • It may be needed to set the following variable: export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig

Now the Go code in this project can interface with the libexiv2 library.

The installation process for other operating systems should be similar. Also, this library is tested with golang:1.13-alpine docker image, where the correct version of libexiv2 is installed with apk --update add exiv2-dev.

Usage

Basic usage:

import "github.com/kolesa-team/goexiv"

// Open an image from disk
goexivImg, err := goexiv.Open("/path/to/image.jpg")
if err != nil {
    return err
}

// Write an EXIF comment
err = goexivImg.SetMetadataString("exif", "Exif.Photo.UserComment", "A comment. Might be a JSON string. Можно писать и по-русски!")
if err != nil {
    return err
}

// Write an EXIF short integer
err = goexivImg.SetMetadataShort("exif", "Exif.Photo.ExposureProgram", "2")
if err != nil {
    return err
}

// Read metadata
err = goexivImg.ReadMetadata()
if err != nil {
    return err
}

// Read an EXIF comment
userComment, err := goexivImg.GetExifData().GetString("Exif.Photo.UserComment")
if err != nil {
    return err
}

fmt.Println(userComment)
// "A comment. Might be a JSON string. Можно писать и по-русски!"

// It is advisable to insert this line at the end of `goexivImg` lifecycle.  
// see exiv_test.go:Test_GetBytes_Goroutine
runtime.KeepAlive(goexivImg)

Changing the image metadata in memory and returning the updated image (an approach fit for a web service):

// Say we have an image in memory: var img []byte
goexivImg, err := goexiv.OpenBytes(img)
if err != nil {
    return err
}

// Write an IPTC comment
err = goexivImg.SetMetadataString("iptc", "Iptc.Application2.Caption", "A comment. Might be a JSON string.")
if err != nil {
    return err
}

// Get back the modified image, so it can now be further processed (e.g. sent over the network)
img = goexivImg.GetBytes()

Retrieving all metadata keys and values:

img.ReadMetadata()
// map[string]string
exif := img.GetExifData().AllTags()

// map[string]string
iptc := img.GetIptcData().AllTags()

A complete image processing workflow in Go can be organized with the following additional libraries:

About

Go bindings for the exiv2 library. Allows reading and writing EXIF/IPTC metadata to/from JPG, WEBP and PNG images.

License:MIT License


Languages

Language:Go 66.2%Language:C++ 25.5%Language:C 8.3%