使用python自动玩连连看

用python写个连连看外挂,于是just do it。就是识别率略低

###################################################################

#encoding=utf-8

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)




image2 = image2.resize(size).convert('L')
code2 = getCode(image2, size)


assert len(code1) == len(code2),"error"

return compCode(code1, code2)

def mkdir(path):  
   
    # 去除首位空格  
    path=path.strip()  
    # 去除尾部 \ 符号  
    path=path.rstrip("\\")  
   
    # 判断路径是否存在  
    # 存在     True  
    # 不存在   False  
    isExists=os.path.exists(path)  
   
    # 判断结果  
    if not isExists:  
        # 如果不存在则创建目录  
        print path+' 创建成功'  
        # 创建目录操作函数  
        os.makedirs(path)  
        return True  
    else:  
        # 如果目录存在则不创建,并提示目录已存在  
        print path+' had exits'  
        return False  


def splitImage(imageName, pathName, row, col):
cnt = 0    
img = Image.open(imageName)  
ori_w,ori_h = img.size    
row = 4  
col = 4  
 
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  



fileName = '%s/a_%d.png' %(pathName, cnt)  
img.crop((X, Y, X_end, Y_end)).save( fileName )  
cnt+=1  


#颜色空间直方图计算
def pil_image_similarity(filepath1, filepath2):
    from PIL import Image
    import math
    import operator


    image1 = Image.open(filepath1)
    image2 = Image.open(filepath2)


#    image1 = get_thumbnail(img1)
#    image2 = get_thumbnail(img2)


    h1 = image1.histogram()
    h2 = image2.histogram()


    rms = math.sqrt(reduce(operator.add,  list(map(lambda a,b: (a-b)**2, h1, h2)))/len(h1) )
    return rms


def pil_image_similarityDHash(filepath1, filepath2):
image1 = Image.open(filepath1)
image2 = Image.open(filepath2)
dhash = classfiy_dHash(image1, image2 )


sum = 64
penc = (sum - dhash)*1.0/sum
#print dhash, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", filepath1, filepath2
return penc


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 )

sourcePath = "testSplit"
destPath = "testSplitDistinct"
if os.path.exists(sourcePath):
shutil.rmtree(sourcePath)
splitImage("1.bmp", sourcePath, 4,4)
FindOutDistinct(sourcePath, destPath)
FindAndClick(destPath)
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值