def test_one():
"""
opencv的subtract方法
"""
file1 = r"./1.bmp"
file2 = r"./2.bmp"
image1 = cv2.imread(file1)
image2 = cv2.imread(file2)
difference = cv2.subtract(image1, image2)
print(difference)
result = not np.any(difference)
if result is True:
print("两张图片一样")
else:
cv2.imwrite("result.jpg", difference)
print("两张图片不一样")
from PIL import Image
import math
import operator
from functools import reduce
def test_two(img1, img2):
"""
使用PIL的Image,然后使用数学方法计算
"""
image1 = Image.open(img1)
image2 = Image.open(img2)
h1 = image1.histogram()
h2 = image2.histogram()
result = math.sqrt(reduce(operator.add, list(map(lambda a, b: (a-b)**2, h1, h2)))/len(h1))
if result < 500:
print('两张图片一样')
else:
print('两张图片不一样')
from PIL import Image
def test_three(image1_path, image2_path):
"""
使用PIL的Image,比较所有的像素,该方法要求图片大小一致
"""
image1 = Image.open(image1_path)
image2 = Image.open(image2_path)
width, height = image1.size
pixels1 = image1.load()
pixels2 = image2.load()
for x in range(width):
for y in range(height):
if pixels1[x, y] != pixels2[x, y]:
return False
return True