Use python to check whether each group of pictures (which can be pictures in different formats) are exactly the same

Written in the front: I have been doing picture series recently, mainly because these are some simple problems I encountered when doing picture processing. After a little tidying up, I found that I was quite productive, hahahaha. Hopefully I don't get caught by my mentor and find time to start a blog when I'm so busy. die laughing


Sometimes, we need to check two pictures, or whether the pictures in two folders are exactly the same. In many cases, even if the pictures in different folders look the same, they cannot be quickly compared due to the different file suffix formats. In this case, we need a fast and reliable way to check. The method introduced today uses the Pillow library. Use the Pillow library to open and load the two images, then subtract the pixel data of the two images and check if the result is an array of zeros. The two images are identical if all elements of the difference array are zero. The specific steps are as follows.


Table of contents

 〇, preparation work, PIL library installation

1. Compare whether two pictures are exactly the same

2. Compare whether all the pictures in the two groups of folders are exactly the same


 〇, preparation work, PIL library installation

The Pillow library (which is a fork of the Python Imaging Library) can be installed in Python with the following command:

pip install Pillow

Alternatively, it can be installed via scientific computing distributions such as Anaconda or Miniconda. If you use Anaconda, you can use the following command to install the Pillow library:

conda install pillow

1. Compare whether two pictures are exactly the same

from PIL import Image
import numpy as np

# 加载BMP和PNG图像,如果你想比较格式的文件只需要在这里更改后缀即可
bmp_image = Image.open("your_bmp_image.bmp")
png_image = Image.open("your_png_image.png")

# 将图像转换为NumPy数组
bmp_array = np.array(bmp_image)
png_array = np.array(png_image)

# 计算像素值的差异
diff = bmp_array - png_array

# 检查是否存在非零差异
if np.any(diff != 0):
    print("这两个图像不完全相同")
else:
    print("这两个图像完全相同")

2. Compare whether all the pictures in the two groups of folders are exactly the same

import os
from PIL import Image
import numpy as np

# 两个文件夹的路径
folder1 = "/path/to/folder1"
folder2 = "/path/to/folder2"

# 获取两个文件夹中的所有文件名
folder1_files = os.listdir(folder1)
folder2_files = os.listdir(folder2)

# 确保两个文件夹中的文件数量相同
if len(folder1_files) != len(folder2_files):
    print("两个文件夹中的文件数量不同")
else:
    # 对两个文件夹中的文件排序,以确保它们的顺序相同
    folder1_files.sort()
    folder2_files.sort()

    # 逐一比较两个文件夹中的图像
    for i in range(len(folder1_files)):
        # 加载两个图像
        image1 = Image.open(os.path.join(folder1, folder1_files[i]))
        image2 = Image.open(os.path.join(folder2, folder2_files[i]))

        # 将图像转换为NumPy数组,并计算像素值的差异
        array1 = np.array(image1)
        array2 = np.array(image2)
        diff = array1 - array2

        # 检查是否存在非零差异
        if np.any(diff != 0):
            print("第%d张图像不同" % (i+1))
        else:
            print("第%d张图像相同" % (i+1))

When using it, make sure that there are only pictures you need to compare in the two groups of folders, and the file names of the pictures should be the same as possible. If you need to ensure that the order is the same when sorting by file name.

Guess you like

Origin blog.csdn.net/weixin_49030835/article/details/129373768