Use Python to compare two different pictures

Source: Internet
Author: User
Tags diff

 fromPILImportImage fromPILImportImagechopsdefcompare_images (Path_one, Path_two, diff_save_location):"""Compare pictures, if there are different then generate show different pictures @ parameter one: Path_one: The path of the first picture @ parameter two: path_two: The path of the second picture @ parameter three: diff_save_location: Save road of different graphs Diameter"""Image_one=Image.open (path_one) image_two=Image.open (path_two)Try: diff=imagechops.difference (Image_one, Image_two)ifDiff.getbbox () isNone:#There's no difference between the pictures, just exit .            Print(""+" We are the same!")        Else: Diff.save (diff_save_location)exceptValueError as E:text= ("indicates that the picture size is inconsistent with the width of box, refer to API Description: Pastes another image to this image."                "The box argument is either a 2-tuple giving the upper left corner, a 4-tuple defining the left, Upper,"                "Right , and lower pixel coordinate, or None (same as (0, 0)). If a 4-tuple is given, the size of the pasted"                "image must match the size of the region. Use the 2-weft box to avoid the above problems")        Print("' {0} ' {1}". Format (e,text))if __name__=='__main__': Compare_images ('1.png',                   '2.png',                   'we're not the same. png')

Execution Result:

    

The second method:

 fromPILImportImageImportMathImportoperator fromFunctoolsImportReducedefimage_contrast (IMG1, Img2): Image1=Image.open (IMG1) image2=Image.open (IMG2) H1=Image1.histogram () H2=Image2.histogram () result= Math.sqrt (Reduce (Operator.add, list (map) (LambdaB: (a) **2, H1, H2))/Len (H1))returnresultif __name__=='__main__': Img1="./1.png"  #Specify picture PathImg2 ="./2.png"result=image_contrast (IMG1,IMG2)Print(Result)

If the two pictures are exactly equal, the returned result is a floating-point type of "0.0", and if not, the larger the result value is returned.

Also use the above two pictures, the execution result is 38, or relatively small:

 Image

This allows you to invoke the method in an automated test case to assert the result of the execution.

Detailed documentation on the Pillow Library:

Http://pillow.readthedocs.org/en/latest/index.html

Use Python to compare two different pictures

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.