LoginSignup
0
4

More than 3 years have passed since last update.

Python初心者によるタイピング自動化メモ

Last updated at Posted at 2020-09-03

初めての自動化

コピペライティングしないために
説明できないコードは使わない
を自分の心に刻み込みコードを書いています。
そのためプログラム内のコメントを多く入れるようにしてます。

ある有名タイピングゲームによる自動化です。
(タイトルは、利用規約うんぬんがあるので控えさせてもらいます。)

実行環境

windows
python3.6
anaconda3

コード全体

from selenium import webdriver
from time import sleep
import time
from selenium.webdriver.common.action_chains import ActionChains #アクションするために必要
import pyautogui as pa
import pyocr
import pyocr.builders
import cv2
from PIL import Image
#driver開く#
driver = webdriver.Chrome()

#windowサイズ#
driver.set_window_size(800, 800)

#サイトを開く#
driver.get("http://hogehoge")

#サイトが開くまで待機#
sleep(10)

#スタートボタンの座標#
#>>> pyautogui.position()#
'''
start_x = 400
start_y = 523
'''

#スタートボタンクリック#
pa.click(400, 523)
sleep(3)
#コースクリック#
pa.click(400, 523)
sleep(3)
pa.press(" ")
sleep(3)

i = 0
while True:
    if i > 350:
        break
    print(i)
    #スクリーンショット#
    pa.screenshot(imageFilename="sumple.png", region=(282, 498, 200, 20))
    im = cv2.imread('sumple.png')#画像読み込み
    gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)#グレースケール

    #2値化
    im_gray = cv2.imread('sumple.png', 0)#グレースケールとして読み込み
    threshold_val = 100
    ret, thres_im = cv2.threshold(im_gray, threshold_val, 255,cv2.THRESH_BINARY)#2値化
    cv2.imwrite('sumple.png', thres_im)#2値化された画像保存
    im_bw = Image.open('sumple.png')

    #文字認識#
    tool = pyocr.get_available_tools()[0]
    text = tool.image_to_string(im_bw, lang='eng', builder=pyocr.builders.TextBuilder())

    #文字入力#
    print(text)
    pa.typewrite(text, interval = 0.1)

    i += 1

#終了#
input("何か入力")
driver.close()
driver.quit()

終わりに

初めてのコードだったので1つ1つ調べるのに時間が掛かりました。
小さな事でもどんどんアウトプットしていこうと思います。

0
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
4