Xavier Villafaina
Xavier Villafaina

Reputation: 635

Comparing images: Don't take image I write

I have a problem with my code. The code works perfect when it starts looping, but after a number of loops it stops reading new screenshots and just reads the previous made ones.

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

I tried to use time.sleep() and delete the image after reading it but that didn't fix the problem so I have no idea why code doesn't use new screenshots.

When I run the same code on those screenshots without loops I always get the correct result, so why is the loop misbehaving?

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

Upvotes: 1

Views: 324

Answers (1)

Xavier Villafaina
Xavier Villafaina

Reputation: 635

I think I fixed, the problem is with vardiffc1, I need put vardiffc1 = 1 inside loop because if find a image diff = 0.02 set vardiffc1 = 0.02 and in the next loop if find image diff 0.03 can not set new symbolist value because 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

Upvotes: 1

Related Questions