goexiv

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 2, 2023 License: MIT Imports: 4 Imported by: 5

README

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:

Documentation

Index

Constants

View Source
const (
	LogMsgDebug LogMsgLevel = 0
	LogMsgInfo              = 1
	LogMsgWarn              = 2
	LogMsgError             = 3
	LogMsgMute              = 4
)

Variables

View Source
var ErrMetadataKeyNotFound = errors.New("key not found")

Functions

func SetLogMsgLevel added in v1.1.0

func SetLogMsgLevel(level LogMsgLevel)

SetLogMsgLevel Set the log level (outputs to stderr)

Types

type Error

type Error struct {
	// contains filtered or unexported fields
}

func (*Error) Code

func (e *Error) Code() int

func (*Error) Error

func (e *Error) Error() string

type ExifData

type ExifData struct {
	// contains filtered or unexported fields
}

func (*ExifData) AllTags

func (d *ExifData) AllTags() map[string]string

Returns all EXIF tags

func (*ExifData) FindKey

func (d *ExifData) FindKey(key string) (*ExifDatum, error)

func (*ExifData) GetString

func (d *ExifData) GetString(key string) (string, error)

func (*ExifData) Iterator

func (d *ExifData) Iterator() *ExifDatumIterator

Iterator returns a new ExifDatumIterator to iterate over all Exif data.

type ExifDatum

type ExifDatum struct {
	// contains filtered or unexported fields
}

func (*ExifDatum) Key

func (d *ExifDatum) Key() string

Key returns the Exif key of the datum.

func (*ExifDatum) String

func (d *ExifDatum) String() string

type ExifDatumIterator

type ExifDatumIterator struct {
	// contains filtered or unexported fields
}

ExifDatumIterator wraps the respective C++ structure.

func (*ExifDatumIterator) HasNext

func (i *ExifDatumIterator) HasNext() bool

HasNext returns true as long as the iterator has another datum to deliver.

func (*ExifDatumIterator) Next

func (i *ExifDatumIterator) Next() *ExifDatum

Next returns the next ExifDatum of the iterator or nil if iterator has reached the end.

type Image

type Image struct {
	// contains filtered or unexported fields
}

func Open

func Open(path string) (*Image, error)

Open opens an image file from the filesystem and returns a pointer to the corresponding Image object, but does not read the Metadata. Start the parsing with a call to ReadMetadata()

func OpenBytes

func OpenBytes(input []byte) (*Image, error)

OpenBytes opens a byte slice with image data and returns a pointer to the corresponding Image object, but does not read the Metadata. Start the parsing with a call to ReadMetadata()

func (*Image) ExifStripKey added in v1.2.0

func (i *Image) ExifStripKey(key string) error

func (*Image) GetBytes

func (i *Image) GetBytes() []byte

Returns an image contents. If its metadata has been changed, the changes are reflected here.

func (*Image) GetExifData

func (i *Image) GetExifData() *ExifData

func (*Image) GetIptcData

func (i *Image) GetIptcData() *IptcData

func (*Image) GetXmpData

func (i *Image) GetXmpData() *XmpData

GetXmpData returns the XmpData of an Image.

func (*Image) ICCProfile

func (i *Image) ICCProfile() []byte

ICCProfile returns the ICC profile or nil if the image doesn't has one.

func (*Image) IptcStripKey added in v1.2.0

func (i *Image) IptcStripKey(key string) error

func (*Image) PixelHeight

func (i *Image) PixelHeight() int64

PixelHeight returns the height of the image in pixels

func (*Image) PixelWidth

func (i *Image) PixelWidth() int64

PixelWidth returns the width of the image in pixels

func (*Image) ReadMetadata

func (i *Image) ReadMetadata() error

ReadMetadata reads the metadata of an Image

func (*Image) SetExifString

func (i *Image) SetExifString(key, value string) error

func (*Image) SetIptcShort added in v1.0.1

func (i *Image) SetIptcShort(key, value string) error

func (*Image) SetIptcString

func (i *Image) SetIptcString(key, value string) error

func (*Image) SetMetadataShort added in v1.0.1

func (i *Image) SetMetadataShort(format, key, value string) error

Sets an exif or iptc key with a given short value

func (*Image) SetMetadataString

func (i *Image) SetMetadataString(format, key, value string) error

Sets an exif or iptc key with a given string value

func (*Image) StripKey added in v1.2.0

func (i *Image) StripKey(f MetadataFormat, key string) error

func (*Image) XmpStripKey added in v1.2.0

func (i *Image) XmpStripKey(key string) error

type IptcData

type IptcData struct {
	// contains filtered or unexported fields
}

func (*IptcData) AllTags

func (d *IptcData) AllTags() map[string]string

Returns all IPTC tags

func (*IptcData) FindKey

func (d *IptcData) FindKey(key string) (*IptcDatum, error)

func (*IptcData) GetString

func (d *IptcData) GetString(key string) (string, error)

func (*IptcData) Iterator

func (d *IptcData) Iterator() *IptcDatumIterator

Iterator returns a new IptcDatumIterator to iterate over all IPTC data.

type IptcDatum

type IptcDatum struct {
	// contains filtered or unexported fields
}

func (*IptcDatum) Key

func (d *IptcDatum) Key() string

Key returns the IPTC key of the datum.

func (*IptcDatum) String

func (d *IptcDatum) String() string

type IptcDatumIterator

type IptcDatumIterator struct {
	// contains filtered or unexported fields
}

IptcDatumIterator wraps the respective C++ structure.

func (*IptcDatumIterator) HasNext

func (i *IptcDatumIterator) HasNext() bool

HasNext returns true as long as the iterator has another datum to deliver.

func (*IptcDatumIterator) Next

func (i *IptcDatumIterator) Next() *IptcDatum

Next returns the next IptcDatum of the iterator or nil if iterator has reached the end.

type LogMsgLevel added in v1.1.0

type LogMsgLevel int

type MetadataFormat added in v1.2.0

type MetadataFormat int
const (
	EXIF MetadataFormat = iota
	IPTC
	XMP
)

type MetadataProvider

type MetadataProvider interface {
	GetString(key string) (string, error)
}

type XmpData

type XmpData struct {
	// contains filtered or unexported fields
}

XmpData contains all Xmp Data of an image.

func (*XmpData) FindKey

func (d *XmpData) FindKey(key string) (*XmpDatum, error)

FindKey tries to find the specified key and returns its data. It returns an error if the key is invalid. If the key is not found, a nil pointer will be returned

type XmpDatum

type XmpDatum struct {
	// contains filtered or unexported fields
}

XmpDatum stores the info of one xmp datum.

func (*XmpDatum) String

func (d *XmpDatum) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL