使用python自动玩连连看
来源:互联网 发布:wlan网络维护招聘 编辑:程序博客网 时间:2024/05/17 04:55
用python写个连连看外挂,于是just do it。就是识别率略低
###################################################################
#encoding=utf-8
import osimport 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)
0 0
- 使用python自动玩连连看
- python 自动玩 连连看 外挂
- 连连看自动解算程序
- cocos2d-x3.6 连连看自动消除
- 玩家家连连看 v2.5 怎么用
- 百玩不厌!阿达连连看2008新版体验
- 连连看
- 连连看.
- 连连看
- 连连看
- 连连看
- 连连看
- 连连看
- 连连看
- 连连看
- 连连看
- 连连看
- 连连看
- Zookeeper(4)-Zookeeper启动过程
- 英文及数字验证码
- Codeforces 344E Read Time [二分答案]【思维】
- Redis快速入门
- Java中,Map遍历的四种方法
- 使用python自动玩连连看
- Java动态代理以及InvocationHandler中invoke()方法笔记
- springmvc 运行流程
- 微信小程序开发官方文档
- [OpenJudge] 2.6基本算法之动态规划 摘花生
- 产品迭代都做了写什么
- 什么是线速路由器 线速转发
- Java源码解析(附录)(3) —— GenericDeclaration
- 新梦-10月的征途