pixelmatch

package module
v0.0.0-...-8d350b6 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 5 Imported by: 8 modules, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

pixelmatch

A Go port of mapbox/pixelmatch, compatible with v8.0.0.

Comparison uses the OKLab HyAB metric with toe-corrected lightness. Thresholds from older YIQ-based versions can produce different counts. Semi-transparent pixels are compared over a checkerboard by default; use Checkerboard(false) to compare over white.

Installation

library

go get github.com/orisano/pixelmatch

cli

go get github.com/orisano/pixelmatch/cmd/pixelmatch

Library usage

Pass two equally sized image.Image values. Their bounds origins may differ; the output uses the first image's bounds. The Go port also preserves precision when comparing 16-bit images.

var diff image.Image
count, err := pixelmatch.MatchPixel(img1, img2,
    pixelmatch.Threshold(0.1),
    pixelmatch.WriteTo(&diff),
)

Omit WriteTo when only the count is needed.

Upstream option Go option Default / behavior
threshold Threshold(float64) 0.1; smaller is more sensitive. 1 is approximately the black-to-white distance.
includeAA IncludeAntiAlias Off; when supplied, anti-aliased pixels count as differences.
alpha Alpha(float64) 0.1; opacity of unchanged pixels in the grayscale output.
aaColor AntiAliasedColor(color.Color) Yellow.
diffColor DiffColor(color.Color) Red.
diffColorAlt DiffColorAlt(color.Color) Uses diffColor by default; highlights pixels darker in the second image.
diffMask EnableDiffMask Off; when supplied, unchanged and excluded AA pixels stay transparent.
checkerboard Checkerboard(bool) true; controls the background for semi-transparent comparison.
windowSize WindowSize(float64) +Inf; finite values return the largest diff count in any square sliding window.
ignoreMask IgnoreMask([]byte) nil; nonzero bytes skip comparison and draw the pixel as unchanged.

Marker colors use RGB and are always opaque. The ignore mask must contain one byte per pixel in row order (width * height), or MatchPixel returns ErrIgnoreMaskSizeNotMatch. Ignored pixels still participate in neighboring anti-alias detection, as in upstream.

Window sizes are floored and clamped to 1..min(width, height). For example, WindowSize(32) on a 10×2 image uses a 2×2 window. Non-finite sizes return the total count. Windowing changes only the returned count; the output image still shows every difference.

Command line

The positional arguments and exit codes follow upstream:

pixelmatch image1.png image2.png [diff.png] [threshold] [includeAA]
pixelmatch image1.png image2.png diff.png 0.1 false

The CLI prints the differing pixel count and percentage. It exits with 0 for a match, 66 for differences, 65 for different dimensions, 64 for invalid usage, and 1 for file or encoding errors. Without a destination it only prints the count. Use -dest - to stream a PNG to stdout; the count then goes to stderr.

The existing flags remain available, including JPEG output, with additional comparison options:

pixelmatch -checkerboard=false -dest diff.png image1.png image2.png
pixelmatch -windowsize 16 -aa image1.png image2.png
pixelmatch -diffmask -ignoremask ignore.bin -dest diff.png image1.png image2.png

-ignoremask reads raw bytes, one per pixel. -aa includes anti-aliased pixels in the count. Color flags accept #RRGGBB or #RRGGBBAA; output marker alpha is always opaque. Run pixelmatch -h for all flags.

Compatibility tests

go test ./... checks counts and output pixels against reference results generated by upstream v8.0.0. It covers transparency, near-black colors, anti-alias detection, masks, windows, cropped images, and differing bounds origins. Tests require no JavaScript runtime or network access.

To regenerate the reference results:

curl -fsSL https://raw.githubusercontent.com/mapbox/pixelmatch/v8.0.0/index.js -o /tmp/pixelmatch-v8.mjs
node testdata/generate-v8.mjs /tmp/pixelmatch-v8.mjs > testdata/pixelmatch-v8.json

Performance

Benchmarks cover identical, sparse, dense, transparent, cropped, and windowed comparisons, both with and without diff output, plus the PNG fixtures:

go test -run '^$' -bench BenchmarkMatchPixel -benchmem
go test -run '^$' -bench '^BenchmarkMatchPixel/dense/count$' -benchtime=3s \
    -cpuprofile /tmp/pixelmatch.cpu -o /tmp/pixelmatch.test
go tool pprof /tmp/pixelmatch.test /tmp/pixelmatch.cpu

Author

Nao Yonashiro (@orisano)

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrIgnoreMaskSizeNotMatch = errors.New("ignore mask size does not match image width/height")
View Source
var ErrImageSizesNotMatch = errors.New("image sizes do not match")

Functions

func EnableDiffMask

func EnableDiffMask(o *MatchOptions)

func IncludeAntiAlias

func IncludeAntiAlias(o *MatchOptions)

func MatchPixel

func MatchPixel(a, b image.Image, opts ...MatchOption) (int, error)

Types

type MatchOption

type MatchOption func(*MatchOptions)

func Alpha

func Alpha(alpha float64) MatchOption

func AntiAliasedColor

func AntiAliasedColor(c color.Color) MatchOption

func Checkerboard

func Checkerboard(enabled bool) MatchOption

Checkerboard controls the comparison background for semi-transparent pixels. It defaults to true; false composites pixels over white.

func DiffColor

func DiffColor(c color.Color) MatchOption

func DiffColorAlt

func DiffColorAlt(c color.Color) MatchOption

func IgnoreMask

func IgnoreMask(mask []byte) MatchOption

IgnoreMask skips pixels whose mask byte is nonzero. The mask must contain width*height bytes in row order, independent of the images' bounds origins. A nil mask disables masking. Ignored pixels are drawn as unchanged.

func Threshold

func Threshold(threshold float64) MatchOption

func WindowSize

func WindowSize(size float64) MatchOption

WindowSize returns the largest diff count in any N by N sliding window. Finite sizes are floored and clamped to [1, min(width, height)]. Non-finite sizes, including the default +Inf, return the total diff count.

func WriteTo

func WriteTo(img *image.Image) MatchOption

type MatchOptions

type MatchOptions struct {
	// contains filtered or unexported fields
}

Directories

Path Synopsis
cmd
pixelmatch command

Jump to

Keyboard shortcuts

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