“百万英雄”类游戏自动检索问题答案

2018-01-30  本文已影响0人  LizPL

最近经常和父母一边视频一边开着今日头条最近很火的百万英雄答题。然后觉得有必要开发个程序能自动检索答案。

【转载请注明出处:https://www.jianshu.com/p/f3a60d6c3092】

我的开发环境为mac os + 使用语言python。 

以下为源代码:

分为三个部分:

一、采集图像;二、识别其中的问题并转成文字字符串;三、调用百度检索。

#coding=utf-8

import pymouse

import time

import Quartz

import LaunchServices

from Cocoaimport NSURL

import Quartz.CoreGraphicsas CG

from PILimport Image

import pytesseract

import webbrowser

class Positons():

def __init__(self):

self.mouse = pymouse.PyMouse()

def get_point(self):

try:

print('正在采集坐标1,请将鼠标移动到左上角')

print(3)

time.sleep(1)

print(2)

time.sleep(1)

print(1)

time.sleep(1)

(x1,y1) =self.mouse.position()

print('成功!坐标:(%f, %f)' %(x1, y1))

print('')

print('正在采集坐标2,请将鼠标移动到右下角')

print(3)

time.sleep(1)

print(2)

time.sleep(1)

print(1)

time.sleep(1)

(x2,y2) =self.mouse.position()

print('成功!坐标:(%f, %f)' %(x2, y2))

return (x1, y1, x2, y2)

except KeyboardInterrupt:

print('获取失败')

def screenshot(self,path,region =None):

if regionis None:

region = CG.CGRectInfinite

image = CG.CGWindowListCreateImage(

region,

            CG.kCGWindowListOptionOnScreenOnly,

            CG.kCGNullWindowID,

            CG.kCGWindowImageDefault

)

dpi =72

        url = NSURL.fileURLWithPath_(path)

dest = Quartz.CGImageDestinationCreateWithURL(

url,

        LaunchServices.kUTTypePNG,

        1,

        None

        )

properties = {

Quartz.kCGImagePropertyDPIWidth: dpi,

        Quartz.kCGImagePropertyDPIHeight: dpi,

        }

Quartz.CGImageDestinationAddImage(dest, image, properties)

Quartz.CGImageDestinationFinalize(dest)

def trans_pic(self,path):

pytesseract.pytesseract.tesseract_cmd ='/usr/local/Cellar/tesseract/3.05.01/bin/tesseract'

        tessdata_dir_config ='--tessdata-dir "/usr/local/Cellar/tesseract/3.05.01/share/tessdata"'

        text = pytesseract.image_to_string(Image.open(path),lang='chi_sim',config=tessdata_dir_config)

text =''.join(text.split())

return text

a = Positons()

(x1,y1,x2,y2) = a.get_point()

region = CG.CGRectMake(x1,y1,x2,y2)

file_path ="/Users/didi/Safe/Study/MyFun/screeshot.png"

a.screenshot(file_path,region=region)

text = a.trans_pic(file_path)

print text

#url = 'http://www.baidu.com/s?wd='+text

url ='https://www.baidu.com/baidu?wd='+text+'&tn=cnopera&ie=utf-8'

print url

webbrowser.open(url)

上一篇下一篇

猜你喜欢

热点阅读