Typing automation notes by Python beginners

First automation

In order not to copy and write ** Do not use unexplained code ** I am writing the code in my heart. Therefore, I try to put a lot of comments in the program.

Automation by a famous typing game. (I will refrain from the title because there are terms of use.)

Execution environment

windows python3.6 anaconda3

Whole code

from selenium import webdriver
from time import sleep
import time
from selenium.webdriver.common.action_chains import ActionChains #Needed to act
import pyautogui as pa
import pyocr
import pyocr.builders
import cv2
from PIL import Image
#driver open#
driver = webdriver.Chrome()

#window size#
driver.set_window_size(800, 800)

#Open site#
driver.get("http://hogehoge")

#Wait until the site opens#
sleep(10)

#Start button coordinates#
#>>> pyautogui.position()#
'''
start_x = 400
start_y = 523
'''

#Click the start button#
pa.click(400, 523)
sleep(3)
#Course click#
pa.click(400, 523)
sleep(3)
pa.press(" ")
sleep(3)

i = 0
while True:
    if i > 350:
        break
    print(i)
    #screenshot#
    pa.screenshot(imageFilename="sumple.png ", region=(282, 498, 200, 20))
    im = cv2.imread('sumple.png')#Image loading
    gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)#grayscale

    #Binarization
    im_gray = cv2.imread('sumple.png', 0)#Read as grayscale
    threshold_val = 100
    ret, thres_im = cv2.threshold(im_gray, threshold_val, 255,cv2.THRESH_BINARY)#Binarization
    cv2.imwrite('sumple.png', thres_im)#Binarized image storage
    im_bw = Image.open('sumple.png')

    #Character recognition#
    tool = pyocr.get_available_tools()[0]
    text = tool.image_to_string(im_bw, lang='eng', builder=pyocr.builders.TextBuilder())

    #Character input#
    print(text)
    pa.typewrite(text, interval = 0.1)

    i += 1

#End#
input("Enter something")
driver.close()
driver.quit()

At the end

It was my first code, so it took me a long time to check each one. I will continue to output even small things.

Recommended Posts

Typing automation notes by Python beginners
Machine learning summary by Python beginners
Python typing
Answer to AtCoder Beginners Selection by Python3
A textbook for beginners made by Python beginners
Python scraping notes
Python study notes _000
Python learning notes
Python beginner notes
Python dynamic typing
python C ++ notes
Python study notes _005
Python grammar notes
Python Library notes
python personal notes
Beginners practice Python
Python beginner's note
python pandas notes
Python study notes_001
python learning notes
Python3.4 installation notes
Notes for Python beginners with experience in other languages 12 (+1) items by function
Primality test by Python
Python Beginner's Guide (Functions)
missingintegers python personal notes
Communication processing by Python
UI Automation in Python
Python beginners organize heapsort
Python package development notes
Python beginners touch Pytorch (3)
Python ipaddress package notes
python textbook for beginners
[Personal notes] Python, Django
Python Pickle format notes
[Python] pytest-mock Usage notes
Python Dictionary Beginner's Guide
First Python miscellaneous notes
Beamformer response by python
Matlab => Python migration notes
Python beginners touch Pytorch (1)
Python beginners touch Pytorch (2)
Notes around Python3 assignments
Batch thought by beginners
Notes using Python subprocesses
Python Beginner's Guide (Introduction)
OpenCV for Python beginners
Python try / except notes
Python framework bottle notes
[For beginners] Basics of Python explained by Java Gold Part 2
■ Kaggle Practice for Beginners --Introduction of Python --by Google Colaboratory
[For beginners] Basics of Python explained by Java Gold Part 1
Python notes using perl-ternary operator
Speech recognition by Python MFCC
Web scraping notes in python3
Learning flow for Python beginners
EXE Web API by Python
Newcomer training program by Python
Parameter setting by python configparser
Python standard unittest usage notes
Python notes to forget soon
Pin python managed by conda