Giter Site home page Giter Site logo

binhvtle / image-comparison Goto Github PK

View Code? Open in Web Editor NEW

This project forked from romankh3/image-comparison

0.0 0.0 0.0 7.35 MB

Published on Maven Central Java Library that compares 2 images with the same sizes and shows the differences visually by drawing rectangles. Some parts of the image can be excluded from the comparison. Can be used for automation QA tests.

Home Page: https://t.me/romankh3

License: Apache License 2.0

Shell 0.27% Java 99.73%

image-comparison's Introduction

logo-trans Maven_Workflow Maven Central Javadocs BCH compliance PRs Welcome

About

Published on Maven Central and jCenter Java Library that compares 2 images with the same sizes and shows the differences visually by drawing rectangles. Some parts of the image can be excluded from the comparison. Can be used for automation qa tests. The Usages of the image-comparison can be found here Usage Image Comparison

  • Implementation is using only standard core language and platform features, no 3rd party libraries and plagiarized code is permitted.

  • Pixels (with the same coordinates in two images) can be visually similar, but have different values of RGB. 2 pixels are considered to be "different" if they differ more than pixelToleranceLevel(this configuration described below) from each other.

  • The output of the comparison is a copy of actual images. The differences are outlined with red rectangles as shown below.

  • Some parts of the image can be excluded from the comparison and drawn in the result image.

Article about growing image-comparison on habr: How did the test task become a production library

Configuration

All these configurations can be updated based on your needs.

Property Description
threshold The threshold which means the max distance between non-equal pixels. Could be changed according size and requirements to the image.
rectangleLineWidth Width of the line that is drawn the rectangle.
destination File of the result destination.
minimalRectangleSize The number of the minimal rectangle size. Count as (width x height). By default it's 1.
maximalRectangleCount Maximal count of the Rectangles, which would be drawn. It means that would get first x biggest rectangles. Default value is -1, that means that all the rectangles would be drawn.
pixelToleranceLevel Level of the pixel tolerance. By default it's 0.1 -> 10% difference. The value can be set from 0.0 to 0.99.
excludedAreas ExcludedAreas contains a List of Rectangles to be ignored when comparing images.
drawExcludedRectangles Flag which says draw excluded rectangles or not.
fillExcludedRectangles Flag which says fill excluded rectangles or not.
percentOpacityExcludedRectangles The desired opacity of the excluded rectangle fill.
fillDifferenceRectangles Flag which says fill difference rectangles or not.
percentOpacityDifferenceRectangles The desired opacity of the difference rectangle fill.
allowingPercentOfDifferentPixels The percent of the allowing pixels to be different to stay MATCH for comparison. E.g. percent of the pixels, which would ignore in comparison. Value can be from 0.0 to 100.00
differenceRectangleColor Rectangle color of image difference. By default, it's red.
excludedRectangleColor Rectangle color of excluded part. By default, it's green.

Release Notes

Can be found in RELEASE_NOTES.

Usage

Maven

<dependency>
    <groupId>com.github.romankh3</groupId>
    <artifactId>image-comparison</artifactId>
    <version>4.4.0</version>
</dependency>

Gradle

compile 'com.github.romankh3:image-comparison:4.4.0'

To compare two images programmatically

Default way to compare two images looks like:
        //load images to be compared:
        BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources("expected.png");
        BufferedImage actualImage = ImageComparisonUtil.readImageFromResources("actual.png");

        //Create ImageComparison object and compare the images.
        ImageComparisonResult imageComparisonResult = new ImageComparison(expectedImage, actualImage).compareImages();
        
        //Check the result
        assertEquals(ImageComparisonState.MATCH, imageComparisonResult.getImageComparisonState());
Save result image

To save result image, can be used two ways:

  1. add a file to save to constructor. ImageComparison will save the result image in this case.
        //load images to be compared:
        BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources("expected.png");
        BufferedImage actualImage = ImageComparisonUtil.readImageFromResources("actual.png");
        
        // where to save the result (leave null if you want to see the result in the UI)
        File resultDestination = new File( "result.png" );

        //Create ImageComparison object with result destination and compare the images.
        ImageComparisonResult imageComparisonResult = new ImageComparison(expectedImage, actualImage, resultDestination).compareImages();
  1. execute ImageComparisonUtil.saveImage static method
        //load images to be compared:
        BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources("expected.png");
        BufferedImage actualImage = ImageComparisonUtil.readImageFromResources("actual.png");

        //Create ImageComparison object with result destination and compare the images.
        ImageComparisonResult imageComparisonResult = new ImageComparison(expectedImage, actualImage).compareImages();

        //Image can be saved after comparison, using ImageComparisonUtil.
        ImageComparisonUtil.saveImage(resultDestination, imageComparisonResult.getResult()); 

Demo

Demo shows how image-comparison works.

Expected Image

expected

Actual Image

actual

Result

result

Contributing

Please, follow Contributing page.

Code of Conduct

Please, follow Code of Conduct page.

License

This project is Apache License 2.0 - see the LICENSE file for details

Thanks @dee-y for designing this logo

Also if you're interesting - see my other repositories

image-comparison's People

Contributors

romankh3 avatar ak98neon avatar grigaman avatar mkytolai avatar pavelvarenik avatar wandmalfarbe avatar kremenec avatar dve avatar grimsa avatar hexronimo avatar khaldattya avatar kyrylokh avatar codacy-badger avatar dee-y avatar

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.