from PIL import Image
import numpy as np
from skimage.metrics import structural_similarity
def compar_images():
img1 = Image.open(r'D:\demo\20230703012732.png')
img2 = Image.open(r'D:\demo\20230703012741.png')
img2 = img2.resize(img1.size)
# 转为灰度图像
gray1 = np.array(img1.convert("L"))
gray2 = np.array(img2.convert("L"))
# 计算SSIM
ssim = structural_similarity(gray1, gray2, multichannel=True)
# 打印SSIM
print('SSIM:', str(round(ssim*100,2))+'%')
Python计算两张图片相似度SSIM算法
于 2023-07-03 01:38:14 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)