Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework implementation to use exiftool as the facit #9

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 33 additions & 29 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
on:
push:
branches: [ main ]
branches: [main]
pull_request:
name: Test
jobs:
test:
strategy:
matrix:
go-version: [1.21.x,1.22.x]
go-version: [1.21.x, 1.22.x]
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
- name: Install staticcheck
run: go install honnef.co/go/tools/cmd/staticcheck@latest
shell: bash
- name: Install golint
run: go install golang.org/x/lint/golint@latest
shell: bash
- name: Update PATH
run: echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
shell: bash
- name: Checkout code
uses: actions/checkout@v3
- name: Fmt
if: matrix.platform != 'windows-latest' # :(
run: "diff <(gofmt -d .) <(printf '')"
shell: bash
- name: Vet
run: go vet ./...
- name: Staticcheck
run: staticcheck ./...
- name: Lint
run: golint ./...
- name: Test
run: go test -race ./...
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
- name: Install staticcheck
run: go install honnef.co/go/tools/cmd/staticcheck@latest
shell: bash
- name: Install golint
run: go install golang.org/x/lint/golint@latest
shell: bash
- name: Update PATH
run: echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
shell: bash
- name: Checkout code
uses: actions/checkout@v3
- name: Fmt
if: matrix.platform != 'windows-latest' # :(
run: "diff <(gofmt -d .) <(printf '')"
shell: bash
- name: Vet
run: go vet ./...
- name: Staticcheck
run: staticcheck ./...
- name: Lint
run: golint ./...
- name: Test
run: go test -race ./... -coverpkg=./... -coverprofile=coverage.txt -covermode=atomic
- name: Upload coverage reports to Codecov
uses: codecov/[email protected]
with:
token: ${{ secrets.CODECOV_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@

# Dependency directories (remove the comment below to include it)
# vendor/

.DS_Store
54 changes: 52 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,58 @@
[![Tests on Linux, MacOS and Windows](https://github.com/bep/imagemeta/workflows/Test/badge.svg)](https://github.com/bep/imagemeta/actions?query=workflow:Test)
[![Go Report Card](https://goreportcard.com/badge/github.com/bep/imagemeta)](https://goreportcard.com/report/github.com/bep/imagemeta)
[![codecov](https://codecov.io/gh/bep/imagemeta/branch/master/graph/badge.svg)](https://codecov.io/gh/bep/imagemeta)
[![GoDoc](https://godoc.org/github.com/bep/imagemeta?status.svg)](https://godoc.org/github.com/bep/imagemeta)

# Work in progress
## This is about READING image metadata

This is not ready, yet, but the goal is to use this as the library to read image metadata in Hugo. The existing Go libraries I have found is not maintained anymore and too complex to build on top of. The goal of this library is to be relatively small and simple, but I may prove myself wrong.
Writing is not supported, and never will.

I welcome PRs with fixes, but please raise an issue first if you want to add new features.

## Performance

Extracting `EXIF` performs well, ref. the benhcmark below. Note that you can get a significant boost if you only need a subset of the fields (e.g. only the `Orientation`). The last line is with the library that [Hugo](https://github.com/gohugoio/hugo) used before it was replaced with this.

```bash
BenchmarkDecodeExif/bep/imagemeta/exif/jpg/alltags-10 69238 17702 ns/op 4418 B/op 167 allocs/op
BenchmarkDecodeExif/bep/imagemeta/exif/jpg/orientation-10 302263 3831 ns/op 650 B/op 19 allocs/op
BenchmarkDecodeExif/rwcarlsen/goexif/exif/jpg/alltags-10 25791 47415 ns/op 175548 B/op 812 allocs/op
```

Looking at some more extensive tests, testing different image formats and tag sources, we see that the current XMP implementation leaves a lot to be desired (you can provide your own XMP handler if you want).

```bash
BenchmarkDecode/bep/imagemeta/png/exif-10 23444 50991 ns/op 4425 B/op 168 allocs/op
BenchmarkDecode/bep/imagemeta/webp/all-10 2980 399424 ns/op 177917 B/op 2436 allocs/op
BenchmarkDecode/bep/imagemeta/webp/xmp-10 3135 371387 ns/op 139866 B/op 2265 allocs/op
BenchmarkDecode/bep/imagemeta/webp/exif-10 37627 32057 ns/op 38187 B/op 177 allocs/op
BenchmarkDecode/bep/imagemeta/jpg/exif-10 68041 17813 ns/op 4420 B/op 167 allocs/op
BenchmarkDecode/bep/imagemeta/jpg/iptc-10 152806 7684 ns/op 1011 B/op 66 allocs/op
BenchmarkDecode/bep/imagemeta/jpg/xmp-10 3222 371182 ns/op 139860 B/op 2264 allocs/op
BenchmarkDecode/bep/imagemeta/jpg/all-10 2940 394144 ns/op 145267 B/op 2489 allocs/op
```

## When in doubt, Exiftools is right

The output of this library is tested against `exiftool -n -json`. This means, for example, that:

* We use f-numbers and not APEX for aperture values.
* We use seconds and not APEX for shutter speed values.
* EXIF field definitions are fetched from this table: https://exiftool.org/TagNames/EXIF.html
* IPTC field definitions are fetched from this table: https://exiftool.org/TagNames/IPTC.html
* The XMP handling is currently very simple, you can supply your own XMP handler (see the `HandleXMP` option) if you need more.

## Development

Many of the tests depends on generated golden files. To update these, run:

```bash
go generate ./gen
```

Note that you need a working `exiftool` and updated binary in your `PATH` for this to work. This was tested OK with:

```
exiftool -ver
12.76
```
10 changes: 10 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
coverage:
status:
project:
default:
target: auto
threshold: 0.5%
patch: off

comment:
require_changes: true
1 change: 0 additions & 1 deletion exif_fields.go

This file was deleted.

35 changes: 35 additions & 0 deletions exiftype_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions gen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//go:generate go run main.go
package main

import (
"bytes"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)

func main() {
outDir := "testdata_exiftool"
os.RemoveAll(outDir)
if err := os.MkdirAll(outDir, 0o755); err != nil {
log.Fatal(err)
}
base := "../testdata"

if err := filepath.Walk(base, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() || strings.HasPrefix(info.Name(), ".") {
return nil
}

basePath := strings.TrimPrefix(path, base)

var buf bytes.Buffer
cmd := exec.Command("exiftool", path, "-json", "-n", "-g", "-e")
cmd.Stdout = &buf
cmd.Stderr = os.Stderr

if err := cmd.Run(); err != nil {
return err
}

outFilename := filepath.Join(outDir, basePath+".json")
if err := os.MkdirAll(filepath.Dir(outFilename), 0o755); err != nil {
return err
}

if err := os.WriteFile(outFilename, buf.Bytes(), 0o644); err != nil {
return err
}

return nil
}); err != nil {
log.Fatal(err)
}
}
Loading
Loading