webp

package module
v0.0.0-...-31b3009 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: MIT Imports: 11 Imported by: 0

README

go-webp

実行に必要なファイル

このライブラリは purego を使って libwebp の共有ライブラリを動的に読み込みます。事前に以下のファイルをインストールしてください。

OS 必要なファイル
Windows libwebp.dlllibwebpdemux.dll
Linux libwebp.solibwebpdemux.so
macOS libwebp.dyliblibwebpdemux.dylib

使い方

インポート
import "github.com/f0reth/go-webp"
デコード(WebP → image.Image)
f, _ := os.Open("input.webp")
defer f.Close()

img, err := webp.Decode(f)
アニメーション WebP のデコード
f, _ := os.Open("anim.webp")
defer f.Close()

ret, err := webp.DecodeAll(f)
// ret.Image: 各フレームの画像スライス
// ret.Delay: 各フレームの表示時間(ミリ秒)
エンコード(image.Image → WebP)
f, _ := os.Create("output.webp")
defer f.Close()

err := webp.Encode(f, img)
エンコードオプション
err := webp.Encode(f, img, webp.Options{
    Quality:  90,      // 画質 0〜100(デフォルト: 75)
    Lossless: false,   // true にすると可逆圧縮(Quality は無視)
    Method:   4,       // エンコード速度 0(速い)〜6(高品質、デフォルト: 4)
    Exact:    false,   // true にすると透明部分の RGB 値を保持
})
ライブラリの読み込み確認
if err := webp.Dynamic(); err != nil {
    log.Fatal("共有ライブラリが見つかりません:", err)
}

Documentation

Overview

Package webp implements a WEBP image decoder/encoder based on libwebp/libwebpdemux shared libraries used via purego (CGo-free).

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 .

Functions

func Decode

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

Decode reads a WEBP image from r and returns it as an image.Image.

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

func Dynamic() error

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

func Encode

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

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

func Init

func Init()

Init is kept for backward compatibility and does nothing. The dynamic/shared library is loaded during package initialization; use Dynamic to check whether loading succeeded.

Types

type Options

type Options struct {
	// Quality in the range [0,100]. Quality of 100 implies Lossless. Default is 75.
	Quality int
	// Lossless indicates whether to use the lossless compression. Lossless will ignore 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
}

Options are the encoding parameters.

type WEBP

type WEBP struct {
	// Decoded images.
	Image []image.Image
	// Delay times, one per frame, in milliseconds.
	Delay []int
}

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

func DecodeAll

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

DecodeAll reads a WEBP image from r and returns the sequential frames and timing information.

Jump to

Keyboard shortcuts

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