Sikuluxはぶっちゃけ重い
なんたって画像認識に頭のよいものを使ってるから処理が結構重い。
だから検索範囲を絞って絞りまくって最小限にしたい。
その座標ピンポイントに画像Aが見えるまで待て→クリックなaltClickerという関数を作って遊ぶことにした。
しかしキャプチャ画像が相対距離でアプリケーションの左上からどのぐらい離れているのかを知る必要がある。そんなわけでまずは20×20ピクセルの相対座標情報を含んだ小さな画像をキャプチャーしたい
import org.sikuli.util.JythonHelper import datetime, os, sys import shutil import os.path #カレントディレクトリのパスが分かるおまじない JythonHelper.get().addSysPath(getBundlePath()) sys.path.append(os.getcwd()) #フォーカスを移す switchApp(u"(アプリケーションのタイトルとか)") #なにか基準にする画像を予めキャプチャしてそこからの相対距離で左上をわりだす if exists(Pattern("kado-1.png").exact()): tmpreg = find(Pattern("kado-1.png").exact()).getTopLeft() #この場合は基準画像からxに-545yに+5した場所がアプリ左上になっている reg = Region(tmpreg.x - 545, tmpreg.y + 5, 800, 600) topleft = reg.getTopLeft() print "範囲:{0}\n左上:({1},{2})".format(reg,topleft.x,topleft.y) #reg = Region(36,186,800,480) #topleft = Location(41, 269) #組み込み関数のキャプチャモードを開く。この場合範囲は20px固定なので最初のドラッグ始めだけ肝心 captureRegion = selectRegion() #相対座標を割出す locationFromTopLeft = captureRegion.x - topleft.x, captureRegion.y - topleft.y print locationFromTopLeft wait(1.0) #画像を20pxの正方形で保存 img = capture(captureRegion.x, captureRegion.y, 20, 20) name = input(u"恐縮ながら画像の名前をつけてもらいたい") + ".png" shutil.move(img, os.path.join(getBundlePath(), name)) scriptFile = open(os.path.join(getBundlePath(),"scripts.txt"), "a") #同じディレクトリのscripts.txtと適当に名付けたファイルにコピペ用の関数を書き込む w = "altClicker(\"" + name + "\", " + str(locationFromTopLeft[0]) + ", " + str(locationFromTopLeft[1]) + ")" + "\n" scriptFile.write(w) scriptFile.close()
まあざっとこんな感じでaltClicker(“hoge.png”, 203, 114)みたいなテキストができるから本番用のスクリプトで
def altClicker(img, dx, dy): starttime = time.time() while not Region(topleft.x + dx, topleft.y + dy, 20, 20).exists(img): wait(0.1) if time.time() - starttime > 10: initall() click(Location(topleft.x + dx + random.randint(-2,2), topleft.y + dy + random.randint(-2,2))) altClicker("hoge.png", 203, 114)
こうすればUWSCみたいな感覚で簡単に作れる。もっと配列使ってエレガントにやろうとも思ったけどさっと遊ぶにはこれぐらいがわかりやすいだろう。こんな感じでお盆を過ごしていたとさ。