-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
103 lines (87 loc) · 2.48 KB
/
main.go
File metadata and controls
103 lines (87 loc) · 2.48 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Package metadata provides high-level functions to extract
// image generation parameters from image files.
//
// This supports multiple sources, including Fooocus, FooocusPlus
// and RuinedFooocus.
//
// Example usage:
//
// To read metadata, first register the sources you want to enable
// by importing the appropriate package:
//
// import (
// _ "github.com/fkleon/fooocus-metadata/fooocus"
// )
//
// To read metadata from a file, use ExtractFromFile:
//
// path := "fooocus/testdata/fooocus-meta.png"
// meta, err := ExtractFromFile(path)
// fmt.Println(meta.Version) // prints "Fooocus v2.5.5"
//
// To read metadata from a stream, use ExtractFromReader.
// You can provide a hint about the original filepath to
// enable path-based features such as sidecar support:
//
// path := "fooocus/testdata/fooocus-meta.png"
// file, err := os.Open(path)
// defer file.Close()
// meta, err := ExtractFromReader(file, WithPath(path))
// fmt.Println(meta.Version) // prints "Fooocus v2.5.5"
//
// To write metadata, use the individual metadata writers
// provided by each package.
package metadata
import (
"fmt"
"io"
"log/slog"
// Required image decoders
_ "image/jpeg" // JPEG support
_ "image/png" // PNG support
_ "golang.org/x/image/webp" // WebP support
"github.com/fkleon/fooocus-metadata/internal/image"
"github.com/fkleon/fooocus-metadata/types"
)
// ExtractOptions contains the options for the Extract function.
type ExtractOptions struct {
// The path of the file to read image metadata from.
File string
}
type Config struct {
Path string
}
type Option func(*Config)
// To enable path-based extraction features, e.g.
// parsing creation date from file pattern or sidecar support
func WithPath(path string) Option {
return func(cfg *Config) {
cfg.Path = path
}
}
func ExtractFromFile(path string) (params types.StructuredMetadata, err error) {
slog.Info("ExtractFromFile", "path", path)
imageFile, err := image.NewContextFromFile(path)
if err != nil {
return
}
return types.Decode(*imageFile)
}
func ExtractFromReader(reader io.ReadSeeker, opts ...Option) (params types.StructuredMetadata, err error) {
if reader == nil {
return params, fmt.Errorf("input reader is required")
}
// Customise config
cfg := Config{}
for _, opt := range opts {
opt(&cfg)
}
slog.Info("ExtractFromReader", "options", opts)
// Parse image metadata
imageCtx, err := image.NewContextFromReader(reader)
if err != nil {
return
}
imageCtx.Filepath = cfg.Path
return types.Decode(*imageCtx)
}