最近在学习python,看了几个库
今天看同事在玩一个游戏 = = 色弱测试游戏。。。。
于是。。就写了下面的东西 。
==========刚开始学习python有不对的地方欢迎指正==============
http://game.ioxapp.com/eye-test/game.html 游戏地址
http://weibo.com/tv/v/Feu5ek2IU?fid=1034:dd43566db21cf419ef3e7d631be0a89b 视频演示
python 3.6
opencv 3.2
=========思路===========
1.截取屏幕,找到游戏框
2.对框内的图像搜索边框然后取中心点
3.对比中心点颜色
4.找到不同的颜色坐标
5.点击~~~
=======问题===========
我用的macbook pro
所以我是按照我的屏幕像素写的
需要安装好自己的第三份扩展库
opencv的安装可以按照
http://blog.csdn.net/u014017080/article/details/76152650 这里
========code================
from PIL import Image,ImageGrab
from skimage.measure import compare_ssim
import imutils,numpy,time,sys
import cv2
import autopy3 as autopy
def dd(crop_img22):
cv2.imwrite("haha2.png", crop_img22)
img = Image.open('./haha2.png')
img.show()
sys.exit(0)
def diff(a,b):
if a[1] == b[1] and a[2] == b[2] and a[0] == b[0]:
return True
else:
return False
def diff2(a, b):
if a[1] == b[1] and a[2] == b[2] and a[0] == b[0]:
return False
else:
return True
# x = 7
# y = 109
# region = (x,y,x+279,y+251)
#
# x2 = 293
# y2 = 109
# region2 = (x2,y2,x2+279,y2+251)
#
#
# img = Image.open('./test.jpg')
# cropImg = img.crop(region)
# cropImg2 = img.crop(region2)
# img = cv2.imread('test.jpg',0)
# crop_img = img[300:400, 50:300]
# bbox = (22, 440, 430, 800)
# im = ImageGrab.grab(bbox)
# im.show()
time.sleep(3)
while 1:
x = 1010
y = 408
# x2 = 975
# y2 = 181
# img = cv2.imread('./ddddd.png')
# crop_img = img[y:y+990, x:x+1010]
#===============
bbox = (x, y, x+1010, y+990)
img = ImageGrab.grab(bbox)
#
## 把PIL处理的图片给cv2用
pil_image = img
crop_img = cv2.cvtColor(numpy.asarray(pil_image), cv2.COLOR_RGB2BGR)
#=========================================
crop_img2 = crop_img.copy()
#二值化
cv2.threshold(crop_img, 214, 255, 1, crop_img)
crop_img = cv2.cvtColor(crop_img,cv2.COLOR_BGR2GRAY)
# sp = crop_img.shape
#记录轮廓数
i = d = 1
#获取轮廓
crop_img, contours, hierarchy = cv2.findContours(crop_img,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
#产看边界
# cv2.drawContours(crop_img2,contours,-1,(0,0,255),3)
# dd(crop_img2)
#遍历轮廓
for cc in contours:
#求中心圆坐标
M = cv2.moments(cc)
if M["m10"] == 0 or M["m00"] == 0 or M["m01"] == 0 :
continue
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
#绘制中心圆 跟边框
# cv2.drawContours(crop_img2, [cc], -1, (0, 255, 0), 2)
# cv2.circle(crop_img2, (cX, cY), 7, (255, 255, 255), -1)
# cv2.putText(crop_img2, 'text', (cX - 20, cY - 20),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
if i == 1:
a = crop_img2[cY,cX]
d1 = (cX, cY)
if i == 2:
b = crop_img2[cY,cX]
d2 = (cX, cY)
if i == 3:
c = crop_img2[cY,cX]
d3 = (cX, cY)
if diff(a,b) and diff(b,c):
pass
elif diff(b,c) and diff2(a,b):
d = d1
break
elif diff(a,c) and diff2(b,c):
d = d2
break
elif diff(a,b) and diff2(b,c):
d = (cX, cY)
break
if i > 3:
c = crop_img2[cY,cX]
if diff2(a,c):
d = (cX, cY)
break
i +=1
# dd(crop_img)
# dd(crop_img2)
# cv2.circle(img2, d, 7, (255, 255, 255), -1)
# cv2.circle(img, (d[0]+1010,d[1]+445), 7, (255, 255, 255), -1)
# print(d)
x = (d[0]+1010)/2
y = (d[1]+445)/2
# print(x,y)
# aaa = autopy.mouse.get_pos()
# print(aaa)
autopy.mouse.smooth_move(int(x), int(y)) # 平滑移动鼠标(上面那个是瞬间的)
autopy.mouse.click() # 单击
#写入图片并且打开查看
# dd(crop_img2)
=======总结========
把PIL处理的图片给cv2用 bbox = (x, y, x+1010, y+990) img = ImageGrab.grab(bbox) # ## 把PIL处理的图片给cv2用 pil_image = img crop_img = cv2.cvtColor(numpy.asarray(pil_image), cv2.COLOR_RGB2BGR)
cv2.findContours 查找边界
cv2.moments(cc) 获取边界中心点