Python+PIL calculates the similarity of two images and returns the x-coordinate of the first unmatched pixel (with full code)

foreword

A few days ago, I saw an article about the operation of Python+selenium+Super Eagle on the slider verification code. The general idea is as follows:

1. Take a screenshot of the slider verification code

2. Use the API of Super Eagle to process the picture,

3. Return the distance of the slider

I also encountered a similar need a long time ago. At that time, my friend wrote a code for me, which used PIL to analyze the pixel points of two pictures, so as to return the pixel points of the x-axis of the two pictures.

For example, as follows, I have two pictures:

insert image description hereinsert image description here

These two pictures are exactly the same. I deliberately drew a digital display on the second picture. Now I need to know the length of the x-axis by comparing the first picture with the second picture.

insert image description here

Ok, without further ado, let's start the code part

library Install
Pillow pip install Pillow

The specific implementation is as follows from (Wen Xin Yi Yan Annotation):

What this code does is calculate the similarity of two images and return the x-coordinate of the first pixel that does not match.

1. Import Imagethe module, which is PIL (Python Imaging Library)part of and used to process images.

2. A get_gapfunction named is defined, which accepts two image objects image1and image2as parameters to calculate the similarity between two images.

3. In the * get_gapfunction, a variable is defined leftand initialized to 0, which is used to record the x-coordinate of the unmatched pixel.

4. Use two layers of loops to traverse image1each pixel of and compare image2whether it is equal to the pixel at the corresponding position.

5. If two pixels are not equal, call is_pixel_equalthe function to judge whether the two pixels are similar.

6. is_pixel_equalThe function accepts two image objects image1and image2, and the pixel coordinates to be compared (x, y).

7. In the * is_pixel_equal* function, get the color values ​​of two pixels through image1.load()[x, y]and * *.image2.load()[x, y]

8. A threshold is defined thresholdfor judging whether two pixels are similar.

9. If the RGB values ​​of two pixels are equal within the threshold range, return * True*, indicating that the two pixels are similar; otherwise return False.

10 In the main function img_cv, use to Image.openopen two image files 'temp_img1.png'and 'temp_img2.png', and assign them to variables img1_and respectively img2_.

11. Call the * get_gap* function, pass in img1_and img2_as parameters, get the similarity between the two images, and assign the x coordinate of the unmatched pixel to the variable x_pos.

12. Finally, use printprint to call img_cvthe return value of the function, which is the x-coordinate of the pixel that does not match.

It should be noted that this code assumes that the two images are the same size and are compared starting from the upper left corner. If you need to compare images of different sizes, or start the comparison from a different location, you need to modify it accordingly.

Let's take a look before running the code, the approximate pixel is roughly 1063-1625=38

The measurement tool used in the picture is Snipaste, which was introduced before: click me

insert image description hereinsert image description here

Code demo:

insert image description here

full code

# -*- coding: utf-8 -*-
'''
@Time    :2023/8/2 15:06
@作者    :一晌小贪欢
@联系    :187xxxxxx
'''
from PIL import Image


def get_gap(image1, image2):
    left = 0
    for i in range(left, image1.size[0]):
        for j in range(image1.size[1]):
            if not is_pixel_equal(image1, image2, i, j):
                left = i
                return left
    return left


def is_pixel_equal(image1, image2, x, y):
    pixel1 = image1.load()[x, y]
    pixel2 = image2.load()[x, y]
    threshold = 60
    if abs(pixel1[0] - pixel2[0]) < threshold and abs(pixel1[1] - pixel2[1]) < threshold and abs(
            pixel1[2] - pixel2[2]) < threshold:
        return True
    else:
        return False


def img_cv():
    img1_ = Image.open('temp_img1.png')
    img2_ = Image.open('temp_img2.png')
    x_pos = get_gap(img1_, img2_)
    return x_pos


print(img_cv())

I personally feel that this code is still very practical. The original code was used on the slider verification code

I hope everyone has to help

A little programmer dedicated to office automation#

I've seen this, follow + like + bookmark = don't get lost! !

If you want to know more about Python office automation, please pay attention!

Guess you like

Origin blog.csdn.net/weixin_42636075/article/details/132054037