在一位Stack Overflow的会员的帮助下,我有了以下代码:
data = "needle's (which is a png image) base64 code goes here"
decoded = data.decode('base64')
f = cStringIO.StringIO(decoded)
image = Image.open(f)
needle = image.load()
while True:
screenshot = ImageGrab.grab()
haystack = screenshot.load()
if detectImage(haystack, needle):
break
else:
time.sleep(5)
我已经编写了以下代码来检查针是否在堆栈中:
def detectImage(haystack, needle):
counter = 0
for hayrow in haystack:
for haypix in hayrow:
for needlerow in needle:
for needlepix in needlerow:
if haypix == needlepix:
counter += 1
if counter == 980: #the needle has 980 pixels
return True
else:
return False
问题在于我得到了第3行的错误:'PixelAccess'对象不可迭代。
有人建议我将needle和haystack都复制到numpy/scipy数组中。然后,我可以使用一个函数来检查2D数组needle是否在2D数组haystack中。
我需要帮助完成以下事项:
1)将这些数组转换为numpy数组。
2)编写一个函数来检查2D数组needle是否在2D数组haystack中。我的函数无法工作。
这些是图片:
Needle:
![needle](https://istack.dev59.com/7R6YD.webp)
Haystack:
![haystack](https://web.archive.org/web/20130407130033/http://s1.postimg.org/aow57gdny/haystack.jpg)
![haystack](https://web.archive.org/web/20130407130031/http://s1.postimg.org/th80b1a26/haystack.jpg)
for x1 in haystack[0]:
应该改为for x1 in y1:
,而for x2 in needle[0]:
则应该改为for x2 in y2:
?否则你会忽略y
变量(但也许这是有意的)。 - askewchanfor ___ in 2dobject
会给你行。 更好的命名约定可能是for hayrow in haystack ... for haypix in hayrow
。 - askewchan