MCPcopy Create free account
hub / github.com/Mercury7353/PyBench / are_two_images_same

Function are_two_images_same

data/unit_test/tests.py:71–112  ·  view source on GitHub ↗

比较两张图片的相似程度是否超过给定阈值。 参数: img1: PIL.Image.Image - 第一张图片 img2: PIL.Image.Image - 第二张图片 threshold: float - 相似度阈值,默认是0.9(即90%) 返回: bool - 如果两张图片的相似度超过阈值则返回True,否则返回False

(img1, img2,threshold=0.4,tolerance=0.5)

Source from the content-addressed store, hash-verified

69 return False
70
71def are_two_images_same(img1, img2,threshold=0.4,tolerance=0.5):
72 """
73 比较两张图片的相似程度是否超过给定阈值。
74
75 参数:
76 img1: PIL.Image.Image - 第一张图片
77 img2: PIL.Image.Image - 第二张图片
78 threshold: float - 相似度阈值,默认是0.9(即90%)
79
80 返回:
81 bool - 如果两张图片的相似度超过阈值则返回True,否则返回False
82 """
83 # 如果尺寸不同,将img2放缩到img1的尺寸
84 if img1.size != img2.size:
85 img2 = img2.resize(img1.size)
86
87 # 如果模式不同,将img2转换为img1的模式
88 if img1.mode != img2.mode:
89 img2 = img2.convert(img1.mode)
90
91 # 将图像转换为numpy数组
92 img1_np = np.array(img1)
93 img2_np = np.array(img2)
94
95 # 计算每个像素的差异
96 diff_np = np.abs(img1_np - img2_np)
97
98 # 计算允许的误差范围
99 tolerance_value = 255 * tolerance
100
101 # 计算在误差范围内的像素数量
102 within_tolerance = np.all(diff_np <= tolerance_value, axis=-1)
103 similar_pixels = np.count_nonzero(within_tolerance)
104
105 # 计算总像素数量
106 total_pixels = img1_np.shape[0] * img1_np.shape[1]
107
108 # 计算相似度
109 similarity = similar_pixels / total_pixels
110 print(similarity)
111 # 判断相似度是否超过阈值
112 return similarity >= threshold
113
114def is_proportionally_similar(img1, img2, tolerance=0.1):
115 """

Callers 5

test_task_80Function · 0.85
test_task_81Function · 0.85
test_task_82Function · 0.85
test_task_86Function · 0.85
test_task_87Function · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected