fooocus

package module
v0.0.0-...-bd9821d Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2025 License: AGPL-3.0 Imports: 17 Imported by: 0

README

Fooocus metadata

A go package for reading and writing Fooocus metadata:

  • Read embedded metadata for image files generated by Fooocus.
  • Read metadata from the the Private Log file (as fallback in case no embedded metadata is available).
  • Write metadata to PNG which can be loaded into Fooocus through Input Image > Metadata.

Usage

This package is intended to be used programmatically. It includes a command line tool to read metadata from a file, which serves as a usage example.

Compatibility matrix

MIME Source Read Write
JPG Embedded (EXIF) ☑️
JPG Private Log ☑️ N/A
PNG Embedded (PNG tEXt) ☑️ ☑️
PNG Private Log ☑️ N/A
WEBP Embedded (EXIF) ☑️
WEBP Private Log ☑️ N/A
Metadata scheme

Fooocus supports two metadata schemes:

  • fooocus(json) - the native scheme.
  • a1111(plain text) - for compatibility with Civitai.

This package supports only the native fooocus scheme.

Metadata format

There are two known versions of the metadata format:

  • The current metadata format (Fooocus v2.2.0 and newer).
  • The "legacy" metadata format (Fooocus v2.1.0 and older).

This package supports both formats.

Documentation

Overview

Package fooocus implements reading and writing Fooocus metadata (image generation parameters).

To read embedded metadata, use NewImageInfo:

path := "testdata/sample.jpg"
image, err := NewImageInfo(path)
fmt.Println(image.FooocusMetadata.Version) // prints "Fooocus v2.5.5"

To read from the private log file, use ParsePrivateLog:

path := "testdata/log.html"
images, err := ParsePrivateLog(privateLogFile)
meta := images["fooocus-meta.jpeg"]
fmt.Println(image.FooocusMetadata.Version) // prints "Fooocus v2.5.5"

To write metadata into a PNG, use EmbedMetadataAsPngText:

meta := &Metadata{}
target, err := os.OpenFile("out.png", os.O_CREATE|os.O_WRONLY, 0644)
err = EmbedMetadataAsPngText(nil, target, meta)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EmbedMetadataAsPngText

func EmbedMetadataAsPngText(source *os.File, target *os.File, meta *Metadata) (err error)

func ParsePrivateLog

func ParsePrivateLog(filePath string) (map[string]*Metadata, error)

Types

type AdmGuidance

type AdmGuidance [3]float32

string: python tuple (p: float, n: float, e: float)

func (*AdmGuidance) MarshalJSON

func (a *AdmGuidance) MarshalJSON() ([]byte, error)

func (*AdmGuidance) UnmarshalJSON

func (a *AdmGuidance) UnmarshalJSON(p []byte) error

type File

type File struct {
	Path     string
	MIME     string
	FileInfo *os.FileInfo
	// contains filtered or unexported fields
}

func (*File) Ext

func (file *File) Ext() string

func (*File) IsImage

func (file *File) IsImage() bool

func (*File) Name

func (file *File) Name() string

func (*File) Parse

func (file *File) Parse(fin *os.File) (err error)

type ImageFile

type ImageFile struct {
	File
	FooocusMetadata *Metadata
	// contains filtered or unexported fields
}

func NewImageInfo

func NewImageInfo(filePath string) (imageInfo *ImageFile, err error)

type Lora

type Lora struct {
	Name   string
	Weight float32
	Hash   string
}

Encoded as nested list of format: list [string, float32, string] (lora name, lora weight, lora hash)

func (*Lora) MarshalJSON

func (l *Lora) MarshalJSON() ([]byte, error)

func (*Lora) UnmarshalJSON

func (l *Lora) UnmarshalJSON(p []byte) error

type LoraCombined

type LoraCombined Lora

String of format "<name> : <weight>"

func (*LoraCombined) MarshalJSON

func (l *LoraCombined) MarshalJSON() ([]byte, error)

func (*LoraCombined) UnmarshalJSON

func (l *LoraCombined) UnmarshalJSON(p []byte) error

type Metadata

type Metadata struct {
	AdaptiveCfg          float32       `json:"adaptive_cfg,omitempty"`
	AdmGuidance          AdmGuidance   `json:"adm_guidance"`
	BaseModel            string        `json:"base_model"`
	BaseModelHash        string        `json:"base_model_hash"`
	ClipSkip             uint8         `json:"clip_skip"`
	CreatedBy            string        `json:"created_by,omitempty"`
	FreeU                string        `json:"freeu,omitempty"` // string: python tuple (b1: float, b2: float, s1: float, s2: float)
	FullNegativePrompt   []string      `json:"full_negative_prompt,omitempty"`
	FullPrompt           []string      `json:"full_prompt,omitempty"`
	GuidanceScale        float32       `json:"guidance_scale"`
	ImageNumber          uint          `json:"image_number,omitempty"`
	InpaintEngineVersion string        `json:"inpaint_engine_version,omitempty"`
	InpaintMode          string        `json:"inpaint_method,omitempty"`
	LoraCombined1        *LoraCombined `json:"lora_combined_1,omitempty"`
	LoraCombined2        *LoraCombined `json:"lora_combined_2,omitempty"`
	LoraCombined3        *LoraCombined `json:"lora_combined_3,omitempty"`
	LoraCombined4        *LoraCombined `json:"lora_combined_4,omitempty"`
	LoraCombined5        *LoraCombined `json:"lora_combined_5,omitempty"`
	Loras                []Lora        `json:"loras"`
	MetadataScheme       string        `json:"metadata_scheme"`
	NegativePrompt       string        `json:"negative_prompt"`
	Performance          string        `json:"performance"`
	Prompt               string        `json:"prompt"`
	PromptExpansion      string        `json:"prompt_expansion"`
	RefinerModel         string        `json:"refiner_model,omitempty"`
	RefinerModelHash     string        `json:"refiner_model_hash,omitempty"`
	RefinerSwapMethod    string        `json:"refiner_swap_method,omitempty"`
	RefinerSwitch        float32       `json:"refiner_switch"`
	Resolution           Resolution    `json:"resolution"`
	Sampler              string        `json:"sampler"`
	Scheduler            string        `json:"scheduler"`
	Seed                 string        `json:"seed"`
	Sharpness            float32       `json:"sharpness"`
	Steps                uint8         `json:"steps"`
	Styles               Styles        `json:"styles"`
	Vae                  string        `json:"vae"`
	Version              string        `json:"version"`
}

Fooocus metadata scheme (json).

Implemented in Fooocus v2.2.0 and newer.

Reference implementation:

func ParseMetadata

func ParseMetadata(scheme string, parameters string) (*Metadata, error)

type MetadataLegacy

type MetadataLegacy struct {
	AdmGuidance          AdmGuidance   `json:"ADM Guidance"`
	BaseModel            string        `json:"Base Model"`
	CFGMimicking         float32       `json:"CFG Mimicking from TSNR,omitempty"`
	ClipSkip             uint8         `json:"CLIP Skip,omitempty"`
	FooocusV2Expansion   string        `json:"Fooocus V2 Expansion"`
	FreeU                string        `json:"FreeU,omitempty"`
	GuidanceScale        float32       `json:"Guidance Scale"`
	ImageNumber          uint          `json:"Image Number,omitempty"`
	InpaintEngineVersion string        `json:"Inpaint Engine Version,omitempty"`
	InpaintMode          string        `json:"Inpaint Mode,omitempty"`
	Lora1                *LoraCombined `json:"LoRA 1,omitempty"`
	Lora2                *LoraCombined `json:"LoRA 2,omitempty"`
	Lora3                *LoraCombined `json:"LoRA 3,omitempty"`
	Lora4                *LoraCombined `json:"LoRA 4,omitempty"`
	Lora5                *LoraCombined `json:"LoRA 5,omitempty"`
	Lora6                *LoraCombined `json:"LoRA 6,omitempty"`
	NegativePrompt       string        `json:"Negative Prompt"`
	OverWriteSwitch      float32       `json:"Overwrite Switch,omitempty"`
	Performance          string        `json:"Performance"`
	Prompt               string        `json:"Prompt"`
	RefinerModel         string        `json:"Refiner Model"`
	RefinerSwapMethod    string        `json:"Refiner Swap Method,omitempty"`
	RefinerSwitch        float32       `json:"Refiner Switch"`
	Resolution           Resolution    `json:"Resolution"`
	Sampler              string        `json:"Sampler"`
	Scheduler            string        `json:"Scheduler"`
	Seed                 int           `json:"Seed"`
	Sharpness            float32       `json:"Sharpness"`
	Steps                uint8         `json:"Steps,omitempty"` // TODO: post process default from Performance
	Styles               Styles        `json:"Styles"`
	Vae                  string        `json:"VAE,omitempty"`
	Version              string        `json:"Version"`
}

Fooocus legacy metadata schema (json).

This format is only found in the private log HTML file generated by Fooocus v2.1.0 and older.

Reference implementation:

type Resolution

type Resolution [2]uint16

string: python tuple (width: int, height: int)

func (*Resolution) MarshalJSON

func (r *Resolution) MarshalJSON() ([]byte, error)

func (*Resolution) UnmarshalJSON

func (r *Resolution) UnmarshalJSON(p []byte) error

type Styles

type Styles []string

Styles are encoded within a string using single-quoted values, e.g.: "['Fooocus V2', 'Fooocus Enhance', 'Fooocus Sharp']"

func (*Styles) MarshalJSON

func (s *Styles) MarshalJSON() ([]byte, error)

func (*Styles) UnmarshalJSON

func (s *Styles) UnmarshalJSON(p []byte) error

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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