要比较两个不同大小的图像的坐标,可以使用以下步骤:
下面是使用Python和PIL库实现上述步骤的代码示例:
from PIL import Image
def compare_images(img1_path, img2_path):
# 加载图像并获取其宽度和高度
img1 = Image.open(img1_path)
img2 = Image.open(img2_path)
width1, height1 = img1.size
width2, height2 = img2.size
# 调整较小的图像大小
if width1 < width2 or height1 < height2:
img1 = img1.resize((width2, height2))
else:
img2 = img2.resize((width1, height1))
# 比较像素的坐标和值
same_pixels = []
for y in range(height1):
for x in range(width1):
pixel1 = img1.getpixel((x, y))
pixel2 = img2.getpixel((x, y))
if pixel1 == pixel2:
same_pixels.append((x, y))
return same_pixels
# 示例用法
img1_path = 'img1.png'
img2_path = 'img2.png'
same_pixels = compare_images(img1_path, img2_path)
print(same_pixels)
在上述示例中,compare_images
函数接受两个图像的文件路径作为参数,并返回它们相同像素的坐标列表。