import os
import os.path
import shutil
from PIL import Image
from PIL import ImageFilter
from PIL import ImageOps
from pyautogui import *
import time
def getCode(img,size):
result = []
# print("x==",size[0])
# print("y==",size[1]-1)
x_size = size[0]-1#width
y_size = size[1] #high
for x in range(0,x_size):
for y in range(0,y_size):
now_value = img.getpixel((x,y))
next_value = img.getpixel((x+1,y))
if next_value < now_value:
result.append(1)
else:
result.append(0)
return result
def compCode(code1,code2):
num = 0
for index in range(0,len(code1)):
if code1[index] != code2[index]:
num+=1
return num
#This module can classfy the image by dHash
def classfiy_dHash(image1,image2,size=(9,8)):
''' 'image1' and 'image2' is a Image Object.
You can build it by 'Image.open(path)'.
'Size' is parameter what the image will resize to it and then image will be compared to another image by the dHash.
It's 9 * 8 when it default.
The function will return the hamming code,less is correct.
'''
image1 = image1.resize(size).convert('L')
code1 = getCode(image1, size)
mkdir(pathName)
for j in range(0, col):
Y = j*ori_h/col
Y_end = Y + ori_h/col
for i in range(0, row):
X = i*ori_w/row
X_end = X + ori_w/row
print X, X_end
def FindOutDistinct(sourcePath, destPath):
picDirDistinct=[]
picDir = []
mkdir(destPath)
for root, dirs, files in os.walk(sourcePath):
for name in files:
picDir.append(os.path.join(root, name))
#print picDir
for i in range(0, len(picDir)):
find = False
for j in range(0, len(picDirDistinct)):
#print i,j
if pil_image_similarity(picDir[i], picDirDistinct[j])< 30:
find = True
print "find ", picDir[i], picDirDistinct[j]
break
if not find:
picDirDistinct.append(picDir[i])
print picDirDistinct
for item in picDirDistinct:
shutil.copy(item, destPath)
def FindAndClick(destPath):
picDir = []
mkdir(destPath)
for root, dirs, files in os.walk(destPath):
for name in files:
picDir.append(os.path.join(root, name))
while True:
T = []
for item in picDir:
print item
PT = []
for pos in locateAllOnScreen(item, grayscale=True):
PT.append(pos)
if 0 != len(PT):
T.append(PT)
print PT
for i in range(0, len(PT)):
for j in range(i+1, len(PT)):
click(center(PT[i]))
time.sleep( 2 )
click(center(PT[j]))
time.sleep( 2 )
print T
if 0 == len(T):
break
time.sleep( 10 )