webp

package module
v0.6.4 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2026 License: MIT Imports: 15 Imported by: 18

README

webp

Status Go Reference

Go encoder/decoder for WebP Image File Format with support for animated WebP images.

Based on libwebp compiled to WASM and transpiled to pure Go with wasm2go (CGo-free).

The library will first try to use a dynamic/shared library (if installed) via purego and will fall back to the transpiled Go.

Build tags

  • nodynamic - do not use dynamic/shared library (use only the transpiled Go)

Documentation

Overview

Package webp implements an WEBP image decoder based on libwebp compiled to WASM.

Index

Constants

View Source
const DefaultMethod = 4

DefaultMethod is the default method encoding parameter.

View Source
const DefaultQuality = 75

DefaultQuality is the default quality encoding parameter.

Variables

View Source
var (
	ErrMemRead  = errors.New("webp: mem read failed")
	ErrMemWrite = errors.New("webp: mem write failed")
	ErrDecode   = errors.New("webp: decode failed")
	ErrEncode   = errors.New("webp: encode failed")
)

Errors .

View Source
var ErrNoExif = errors.New("webp: no exif data")

ErrNoExif is returned by DecodeExif when the WEBP has no EXIF chunk.

Functions

func Decode

func Decode(r io.Reader, opts ...Options) (image.Image, error)

Decode reads a WEBP image from r; pass Options{AutoRotate: true} to apply the EXIF orientation.

func DecodeConfig

func DecodeConfig(r io.Reader) (image.Config, error)

DecodeConfig returns the color model and dimensions of a WEBP image without decoding the entire image.

func Dynamic added in v0.2.2

func Dynamic() error

Dynamic returns error (if there was any) during opening dynamic/shared library.

func Encode added in v0.2.4

func Encode(w io.Writer, m image.Image, o ...Options) error

Encode writes the image m to w with the given options.

func EncodeAll added in v0.6.2

func EncodeAll(w io.Writer, anim *WEBP, o ...Options) error

EncodeAll writes the animation anim to w; all frames must share the same bounds.

Types

type Exif added in v0.6.2

type Exif struct {
	// Basic image info
	Orientation int // EXIF orientation (1-8). 1 = normal, values 2-8 indicate rotation/flip.
	Width       int // Image width in pixels.
	Height      int // Image height in pixels.

	// Camera info
	Make     string // Camera manufacturer (e.g., "Canon").
	Model    string // Camera model (e.g., "Canon EOS 5D Mark III").
	Software string // Software used to process/create the image.

	// Date/Time (format: "YYYY:MM:DD HH:MM:SS")
	DateTime         string // File modification date/time.
	DateTimeOriginal string // Original capture date/time (when photo was taken).

	// Exposure settings
	ExposureTime float64 // Shutter speed in seconds (e.g., 0.004 = 1/250s).
	FNumber      float64 // Aperture f-number (e.g., 5.6 = f/5.6).
	ISOSpeed     int     // ISO speed rating (e.g., 800).
	FocalLength  float64 // Lens focal length in millimeters.
	Flash        int     // Flash mode/status (0 = no flash, non-zero = flash fired).

	// GPS location
	GPSLatitude  float64 // Latitude in decimal degrees (positive = North, negative = South).
	GPSLongitude float64 // Longitude in decimal degrees (positive = East, negative = West).
	GPSAltitude  float64 // Altitude in meters above sea level.

	// Copyright/Author
	Copyright string // Copyright notice.
	Artist    string // Creator/photographer name.
}

Exif holds the EXIF metadata decoded from a WEBP image.

func DecodeExif added in v0.6.2

func DecodeExif(r io.Reader) (*Exif, error)

DecodeExif reads the EXIF metadata from a WEBP image. It returns ErrNoExif if the image carries no EXIF chunk.

type Options added in v0.2.4

type Options struct {
	// Quality in the range [0,100]. Default is 75.
	Quality int
	// Lossless enables lossless compression. Lossless ignores quality.
	Lossless bool
	// Method is quality/speed trade-off (0=fast, 6=slower-better). Default is 4.
	Method int
	// Exact preserve the exact RGB values in transparent area.
	Exact bool
	// AutoRotate applies the EXIF orientation to the decoded image (Decode/DecodeAll only).
	AutoRotate bool
}

Options are the encoding parameters, plus AutoRotate which applies to Decode.

type WEBP

type WEBP struct {
	// Decoded images.
	Image []image.Image
	// Delay times, one per frame, in milliseconds.
	Delay []int
	// LoopCount is the number of times the animation repeats (0 = infinite).
	LoopCount int
}

WEBP represents the possibly multiple images stored in a WEBP file.

func DecodeAll

func DecodeAll(r io.Reader, opts ...Options) (*WEBP, error)

DecodeAll returns the sequential frames and timing; pass Options{AutoRotate: true} to orient each frame.

Jump to

Keyboard shortcuts

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