用python写一份两个文件夹相同名字图片对比的脚本
时间: 2023-10-09 13:05:17 浏览: 218
好的,以下是一个用 Python 写的比较两个文件夹中同名图片的脚本:
```python
import os
from PIL import Image
folder1 = './folder1'
folder2 = './folder2'
for filename in os.listdir(folder1):
if filename.endswith('.jpg') or filename.endswith('.jpeg') or filename.endswith('.png'):
if os.path.isfile(os.path.join(folder2, filename)):
img1 = Image.open(os.path.join(folder1, filename))
img2 = Image.open(os.path.join(folder2, filename))
if img1.size == img2.size and img1.mode == img2.mode:
diff = ImageChops.difference(img1, img2)
if diff.getbbox() is None:
print(f'{filename} is identical in both folders')
else:
print(f'{filename} is different in both folders')
else:
print(f'{filename} is different in both folders')
else:
print(f'{filename} does not exist in {folder2}')
```
该脚本会遍历 folder1 文件夹中的所有图片文件,判断其是否也存在于 folder2 文件夹中,如果存在,则会用 PIL 库中的 Image 对象来比较两张图片是否相同,最后输出结果。需要注意的是,该脚本默认两个文件夹中的图片文件名相同。
阅读全文