Skip to main content

compares two images for equality or a difference percentage

Project description

imgcompare

Logo

License: MIT

Calculates the difference between images in percent, checks equality with optional fuzzyness.

Algorithm

  • Get diff image using Pillow's ImageChops.difference
  • Convert the diff image to greyscale
  • Sum up all diff pixels by summing up their histogram values
  • Calculate a percentage based on a black and white image of the same size

Check the tests to see example diffs for different scenarios. Don't expect the diff of two jpg images be the same for the same images converted to png. Don't do interformat compares (e.g. JPG with PNG).

CAUTION: Due to a bugfix in the RGB -> L (Luminosity Greyscale) conversion in Pillow 7.0.0 the calculated outputs (percentages, etc.) differ between 6.2.2 and 7.0.0.

https://github.com/datenhahn/imgcompare/pull/3#issuecomment-576053494

Dependencies

  • Pillow ( https://python-pillow.org/ )
    • imgcompare 0.1.0: no version checks
    • imgcompare 1.0.0: Python 2.7, Pillow <= 6.2.2
    • imgcompare 2.0.0: Python 3.x, Pillow >= 7.0.0, (calculated percentages will differ to Pillow 6.2.2)

Installation

pip install imgcompare

Usage

compare images

is_same = is_equal("image_a.jpg", "image_b.jpg")

use the tolerance parameter to allow a certain diff pass as same

is_same = is_equal("image_a.jpg", "image_b.jpg", tolerance=2.5)

get the diff percentage

percentage = image_diff_percent("image_a.jpg", "image_b.jpg")

or work directly with pillow image instances (also works for is_equal)

image_a = Image.open("image_a.jpg")
image_b = Image.open("image_b.jpg")
percentage = image_diff_percent(image_a, image_b)

Examples

On Image B the cat wears some fancy goggles which results in a small difference.

Slight Difference

Image A

Cat

Image B

Cat Slight Different

imgcompare.image_diff_percent(JPG_CAT, JPG_CAT_SLIGHT_DIFF)

result => 0.344547385621

Difference between jpg and same jpg encoded again

Image B is Image A, but run again through the JPEG encoder, so Image B now has slightly more compression artefacts than Image A, which results in a small diff.

Image A

Cat

Image B

Cat Reencoded

imgcompare.image_diff_percent(JPG_CAT, JPG_CAT_REENCODED)

result => 0.0149232026144

Black and White

When comparing a fully black image with a fully white one, the expected difference is 100%.

Image A

Black

Image B

White

imgcompare.image_diff_percent(JPG_BLACK, JPG_WHITE)

result => 100.0

Black and Half-Black-White

Comparing a full black image with a half black, half white one gives a difference of 50%.

Image A

Black

Image B

Half Black White

imgcompare.image_diff_percent(JPG_BLACK, JPG_HALF_BW)

result => 50.0

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

imgcompare-2.0.1-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file imgcompare-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: imgcompare-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.9

File hashes

Hashes for imgcompare-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f4ca88b68392d54b92b3f60b1157e058b3cb7b52ce84ea44dff522a1da39e800
MD5 12a8729ba3fe65e540d88ea1c5b2f6be
BLAKE2b-256 445bd4fbdf996d9b99a1935e85fc28caa24a5048fc60cd06c028a32ba39de28e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page