Use python to compare the differences between the two images,

Source: Internet
Author: User

Use python to compare the differences between the two images,

From PIL import Imagefrom PIL import ImageChops def compare_images (path_one, path_two, diff_save_location): "" compare images. If there are differences, different images are generated and displayed. @ parameter 1: path_one: path of the first Image @ parameter 2: path_two: Path of the second Image @ parameter 3: diff_save_location: Save path of different images "" image_one = Image. open (path_one) image_two = Image. open (path_two) try: diff = ImageChops. difference (image_one, image_two) if diff. getbbox () is None: # exit print ("[+] We Are the same! ") Else: diff. save (diff_save_location) failed t ValueError as e: text = ("indicates that the image size and the width of the box are inconsistent. For details, refer to API Description: Pastes another image into 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 a 2-latitude box to avoid the above problem ") print (" [{0}] {1 }". format (e, text) if _ name _ = '_ main _': compare_images('1.png ', '2.png', 'We are not like .png ')

Execution result:

Method 2:

From PIL import Imageimport mathimport operatorfrom functools import performancedef image_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 (lambda a, B: (a-B) ** 2, h1, h2)/len (h1 )) return resultif _ name _ = '_ main _': img1 = ". /1.png" # specify the image path img2 = ". /2.png" result = image_contrast (img1, img2) print (result)

If the two images are completely equal, the returned results are of the floating point type "0.0". If they are different, the returned results are larger.

Similarly, with the above two images, the execution result is 38, which is relatively small:

In this way, you can call this method in an automated test case to assert the execution result.

Detailed documentation on the Pillow database:

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

Summary

The above section describes how to use python to compare the two images. I hope it will be helpful to you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.