【发布时间】:2013-03-29 20:35:54
【问题描述】:
所以在堆栈溢出成员的帮助下,我有以下代码:
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' object is not iterable
有人建议我将 needle 和 haystack 复制到 numpy/scipy 数组中会更容易。然后我可以使用一个函数来检查 2D 数组 needle 是否在 2D 数组 haystack 内。
我需要帮助:
1) 将这些数组转换为 numpy 数组。
2) 检查二维数组针是否在二维数组干草堆内的函数。我的功能不起作用。
【问题讨论】:
-
也许这一行:
for x1 in haystack[0]:应该说是for x1 in y1:。和for x2 in needle[0]:应该是for x2 in y2:?否则,您将忽略y变量(但也许这是故意的)。 -
哦,对了。你是对的。
-
记住
for ___ in 2dobject会给你行。更好的命名约定可能是for hayrow in haystack ... for haypix in hayrow -
haypix 中的 pix 代表什么?
-
好的,我已经改了。谢谢你。
标签: python python-2.7 numpy scipy detection