在图像中查找图像

时间:2014-03-24 20:06:29

标签: python python-imaging-library

def boardcords(img1, img2):
    img1 = asarray(img1) #what your looking for
    img2 = asarray(img2) #big
    img2y=img2.shape[0]
    img2x=img2.shape[1]
    stopy=img2y-img1.shape[0]+1
    stopx=img2x-img1.shape[1]+1
    for x1 in range(0,stopx):
        for y1 in range(0,stopy):
            x2=x1+img1.shape[1]
            y2=y1+img1.shape[0]
            pic=img2[y1:y2,x1:x2]
            test=pic==img1
            if test.all():
                return x1, y1
bx, by = boardcords(Image.open('small.png'),Image.open('test.png'))
print (bx, by)
bx2, by2 = boardcords((Image.open('small2.png')),Image.open('test.png'))
print (bx2, by2)

这有时仅出于某种原因。它成功找到第一个测试的图像,但不是第二个,发出以下错误

AttributeError: 'bool' object has no attribute 'all'

http://s000.tinyupload.com/index.php?file_id=08383055927431033826

必要时的图片

1 个答案:

答案 0 :(得分:0)

        test=pic==img1
        if test.all():
            return x1, y1

pic==img1的类型为bool,因此test的类型为bool,然后您尝试调用其all方法,该方法不存在

如果将其替换为if test:

,它会起作用吗?