
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
A C++17 port of the JavaScript pixelmatch library (with python binding), providing a small pixel-level image comparison library.
install
pip install pybind11-pixelmatch
diff image
pip install fire # for CLI
python3 -m pybind11_pixelmatch --help
python3 -m pybind11_pixelmatch data/pic1.png data/pic2.png diff_output.png
label | image |
---|---|
img1 | ![]() |
img2 | ![]() |
diff | ![]() |
# diffColor -> delete
# diffColorAlt -> insert
python3 -m pybind11_pixelmatch data/pic1.png data/pic2.png --output data/diff.png --diffColor='#ff0000' --diffColorAlt='#00ff00'
label | image |
---|---|
img1 | ![]() |
img2 | ![]() |
diff | ![]() |
If you want a pure python package, then try
pip install pixelmatch
. But it's much slower.
A C++17 port of the JavaScript pixelmatch library, providing a small pixel-level image comparison library.
Features accurate anti-aliased pixels detection and perceptual color difference metrics.
Based on mapbox/pixelmatch. pixelmatch-cpp17 is around 300 lines of code, and has no dependencies, operating on RGBA-encoded buffers.
pixelmatch::Options options;
options.threshold = 0.1f;
const std::vector<uint8_t> img1 = ...;
const std::vector<uint8_t> img2 = ...;
std::vector<uint8_t> diffImage(img1.size());
const int numDiffPixels = pixelmatch::pixelmatch(img1, img2, diffImage, width, height, stride, options);
Compared to mapbox/pixelmatch-cpp, pixelmatch-cpp17 ports the latest features from the JavaScript library, and is built with production-grade practices, including thorough test coverage and fuzz-testing. Build files are included for Bazel, but contributions for other build systems are welcome.
Implements ideas from the following papers:
expected | actual | diff |
---|---|---|
![]() | ![]() | ![]() |
![]() | ![]() | ![]() |
![]() | ![]() | ![]() |
img1
, img2
— Image data of the images to compare, as a RGBA-encoded byte array. Note: image dimensions must be equal.output
— Image data to write the diff to, or std::nullopt
if you don't need a diff image.width
, height
— Width and height of the images. Note that all three images need to have the same dimensions.strideInPixels
— Stride of the images. Note that all three images need to have the same stride.options
is a struct with the following fields:
threshold
— Matching threshold, ranges from 0.0f
to 1.0f
. Smaller values make the comparison more sensitive. 0.1
by default.includeAA
— If true
, disables detecting and ignoring anti-aliased pixels. false
by default.alpha
— Blending factor of unchanged pixels in the diff output. Ranges from 0
for pure white to 1
for original brightness. 0.1
by default.aaColor
— The color of anti-aliased pixels in the diff output as an RGBA color. (255, 255, 0, 255)
by default.diffColor
— The color of differing pixels in the diff output as an RGBA color (255, 0, 0, 255)
by default.diffColorAlt
— An alternative color to use for dark on light differences to differentiate between "added" and "removed" parts. If not provided, all differing pixels use the color specified by diffColor
. std::nullopt
by default.diffMask
— Draw the diff over a transparent background (a mask), rather than over the original image. Will not draw anti-aliased pixels (if detected).Compares two images, writes the output diff and returns the number of mismatched pixels.
Add the following to your WORKSPACE
file:
git_repository(
name = "pixelmatch-cpp17",
branch = "main",
remote = "https://github.com/jwmcglynn/pixelmatch-cpp17",
)
Then add a dependency on @pixelmatch-cpp17
:
cc_test(
name = "my_test",
# ...
data = glob([
"testdata/*.png",
]),
deps = [
"@pixelmatch-cpp17",
# ...
],
)
In your test file, include pixelmatch with:
#include <pixelmatch/pixelmatch.h>
FAQs
A C++17 port of the JavaScript pixelmatch library (with python binding), providing a small pixel-level image comparison library.
We found that pybind11-pixelmatch demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.