GithubHelp home page GithubHelp logo

nicolashahn / diffimg Goto Github PK

View Code? Open in Web Editor NEW
209.0 12.0 26.0 3.27 MB

Differentiate images in python - get a ratio or percentage difference, and generate a diff image

License: MIT License

Python 90.80% Shell 9.20%
python image-processing diffing testing-tools

diffimg's Introduction

diffimg

Get the % difference in images using PIL's histogram + generate a diff image. Images should have the same color channels (for example, RGB vs RGBA). If the image dimensions differ, the 2nd image will be resized to match the first before calculating the diff.

PyPI version

Installation

Now available from PyPi: pip install diffimg

Usage

>>> from diffimg import diff
>>> diff('mario-circle-cs.png', 'mario-circle-node.png')
0.007319618135968298

The very simple diff function returns a raw ratio instead of a % by default.

diff(im1_file, 
     im2_file, 
     delete_diff_file=False, 
     diff_img_file=DIFF_IMG_FILE
     ignore_alpha=False)

im1_file, im2_file: filenames of images to diff.

delete_diff_file: a file showing the differing areas of the two images is generated in order to measure the diff ratio with the same dimensions as the first image. Setting this to True removes it after calculating the ratio.

diff_img_file: filename for the diff image file. Defaults to diff_img.png (regardless of inputed file's types).

ignore_alpha: ignore the alpha channel for the ratio and if applicable, sets the alpha of the diff image to fully opaque.

As command line tool

python -m diffimg image1 image2 [-r/--ratio] [-d/--delete] [-f/--filename DIFF_IMG_FILE]

--ratio outputs a number between 0 and 1 instead of the default Images differ by X%.

--delete removes the diff file after retrieving ratio/percentage.

--filename specifies a filename to save the diff image under. Must use a valid extension.

--ignore-alpha ignore the alpha channel.

Tests

$ ./test.py
......
----------------------------------------------------------------------
Ran 6 tests in 0.320s

OK

Formula

The difference is defined by the average % difference between each of the channels (R,G,B,A?) at each pair of pixels Axy, Bxy at the same coordinates in each of the two images (why they need to be the same size), averaged over all pairs of pixels.

For example, compare two 1x1 images A and B (a trivial example, >1 pixels would have another step to find the average of all pixels):

A1,1 = RGB(255,0,0) (pure red)

B1,1 = RGB(100,0,0) (dark red)

((255-100)/255 + (0/0)/255 + (0/0)/255))/3 = (155/255)/3 = 0.202614379

So these two 1x1 images differ by 20.2614379% according to this formula.

Sample image 1

Alt text

Sample image 2

Alt text

Resulting diff image

Alt text

Difference percentage output

Images differ by 0.731961813597%

diffimg's People

Contributors

nicolashahn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

diffimg's Issues

Difference ratio calculated incorrectly

Hey! Thanks for this repo, it pointed me in the direction of PIL which I could use to solve a similar sort of problem ๐Ÿ˜„.

One thing I noticed was that the difference was calculated based on a factor of 100 as the maximum difference:

# Calculate difference as a ratio.
stat = ImageStat.Stat(diff_img)
# Can be [r,g,b] or [r,g,b,a].
sum_channel_values = sum(stat.mean)
max_all_channels = len(stat.mean) * 100
diff_ratio = sum_channel_values/max_all_channels

But for normal RGB the values are in the range 0-255, so the difference could be up to 255. You can repro this with a script that generates black and white images and diffs them (which should print 1.0):

>>> import diffimg
>>> import PIL
>>> PIL.Image.new('RGB', (10,10), (0,0,0)).save('a.png')
>>> PIL.Image.new('RGB', (10,10), (255,255,255)).save('b.png')
>>> diffimg.diff('a.png', 'b.png')
2.55

difference image is not convenient for png

I am using this tool to measure the difference between two png files. The alpha of each pixel of both images is 255. Consequently the diff image produced by ImageChops.difference() is completely transparent. This is not convenient to locate differences between the two images.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.