我的代码有问题。该代码在开始循环时可以完美地工作,但是经过多次循环后,它将停止读取新的屏幕截图,而仅读取先前制作的屏幕截图。
loop 1: image 2c: result 2c
loop 2: image Qd: result Qd
...
loop 10: image Td: result Td
loop 11: image As: result Td
loop 12: image 3s: result Td
我尝试使用
time.sleep()
并在读取图像后将其删除,但这无法解决问题,因此我不知道为什么代码不使用新的屏幕截图。当我在没有循环的那些屏幕快照上运行相同的代码时,我总是得到正确的结果,那么循环为什么会出现异常?
import pyscreenshot as ImageGrab
from itertools import izip
from PIL import Image
import time
symbolslist = ["2c","3c","4c","5c","6c","7c","8c","9c","Tc","Jc","Qc","Kc","Ac","2h","3h","4h","5h","6h","7h","8h","9h","Th","Jh","Qh","Kh","Ah","2d","3d","4d","5d","6d","7d","8d","9d","Td","Jd","Qd","Kd","Ad","2s","3s","4s","5s","6s","7s","8s","9s","Ts","Js","Qs","Ks","As"]
varc1 = 1
vardiffc1 = 1
varc2 = 1
vardiffc2 = 1
onoff=1
while onoff < 2:
c=0
while c<len(symbolslist):
#Screenshot and save
imc1=ImageGrab.grab(bbox=(367,277,383,312)) # X1,Y1,X2,Y2 21 24
imc1.save("c1.png","png")
time.sleep(0.6)
i1 = Image.open("c1.png")
i2 = Image.open("images/c1/" +symbolslist[c] +".png")
assert i1.mode == i2.mode, "Different kinds of images."
assert i1.size == i2.size, "Different sizes."
pairs = izip(i1.getdata(), i2.getdata())
if len(i1.getbands()) == 1:
# for gray-scale jpegs
dif = sum(abs(p1-p2) for p1,p2 in pairs)
else:
dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))
ncomponents = i1.size[0] * i1.size[1] * 3
diff = (dif / 255.0 * 100) / ncomponents
#print "Difference (percentage):", (dif / 255.0 * 100) / ncomponents
#print diff
if diff <= vardiffc1:
#print diff
#print vardiff
varc1 = symbolslist[c]
vardiffc1 = diff
if diff <= 0.5:
c=len(symbolslist)
else:
pass
else:
pass
c+=1
print varc1
最佳答案
我想我已经解决了,问题是vardiffc1,我需要在循环内放入vardiffc1 = 1,因为如果找到图像diff = 0.02设置vardiffc1 = 0.02,在下一个循环中如果找到图像diff 0.03不能设置新的符号值,因为vardiffc1 = 0.02
import pyscreenshot as ImageGrab
from itertools import izip
from PIL import Image
import time
symbolslist = ["2c","3c","4c","5c","6c","7c","8c","9c","Tc","Jc","Qc","Kc","Ac","2h","3h","4h","5h","6h","7h","8h","9h","Th","Jh","Qh","Kh","Ah","2d","3d","4d","5d","6d","7d","8d","9d","Td","Jd","Qd","Kd","Ad","2s","3s","4s","5s","6s","7s","8s","9s","Ts","Js","Qs","Ks","As"]
onoff=1
while onoff < 2:
c=0
while c<len(symbolslist):
varc1 = 1
vardiffc1 = 1
varc2 = 1
vardiffc2 = 1
#Screenshot and save
imc1=ImageGrab.grab(bbox=(367,277,383,312)) # X1,Y1,X2,Y2 21 24
imc1.save("c1.png","png")
time.sleep(0.6)
i1 = Image.open("c1.png")
i2 = Image.open("images/c1/" +symbolslist[c] +".png")
assert i1.mode == i2.mode, "Different kinds of images."
assert i1.size == i2.size, "Different sizes."
pairs = izip(i1.getdata(), i2.getdata())
if len(i1.getbands()) == 1:
# for gray-scale jpegs
dif = sum(abs(p1-p2) for p1,p2 in pairs)
else:
dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))
ncomponents = i1.size[0] * i1.size[1] * 3
diff = (dif / 255.0 * 100) / ncomponents
#print "Difference (percentage):", (dif / 255.0 * 100) / ncomponents
#print diff
if diff <= vardiffc1:
#print diff
#print vardiff
varc1 = symbolslist[c]
vardiffc1 = diff
if diff <= 0.5:
c=len(symbolslist)
else:
pass
else:
pass
c+=1
print varc1