一、判断图片是否相等
1.基于相等判断图像是否相同
import operator
from PIL import Image
import os
os.chdir('e:\mediabigdata')
a=Image.open('./img/image0.jpg')
b=Image.open('./img/image0.jpg')
out=operator.eq(a,b)
print(out)
2.基于numpy计算图像是否相似
import numpy as np
from PIL import Image
import os
os.chdir('e:\mediabigdata')
a=Image.open('./img/image0.jpg')
b=Image.open('./img/image0.jpg')
diff=np.subtract(a,b)
out=not np.any(diff)
print(out)
3.基于哈希判断
import hashlib
a=open('./img/image1.jpg','rb')
b=open('./img/image1.jpg','rb')
amd5=hashlib.md5(a.read()).hexdigest()
bmd5=hashlib.md5(b.read()).hexdigest()
print(amd5)
print(bmd5)
4.缩放图像,基于各种哈希判断
import cv2
import numpy as np
import matplotlib.pyplot as plt
import os
# 均值哈希算法
def aHash(img):
# 缩放为8*8
plt.imshow(img)
plt.axis('off') #去掉坐标轴
plt.show()
img = cv2.resize(img, (8, 8))
plt.imshow(img)
plt.axis('off') #去掉坐标轴
plt.show()
# 转换为灰度图
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# s为像素和初值为0,hash_str为hash值初值为''
s = 0
hash_str = ''
# 遍历累加求像素和
for i in range(8):
for j in range(8):
s = s