What do you need
Apart from python, you need to install pyscreenshot and pynput
Go in the cmd or in the terminal (or powerhell) or in the linux shell and write:
pip install pyscreenshot
pip install pynput
Maybe you have to write pip3 for mac os.
Remember not to put the i in pynput, because I do it every time and it will not make you find the module.
What is grabimage?
A very simple program to grab images with the mouse. There are similar apps on windows, but I thought this could be useful for other programs in python where you got to get some images from the computer screen, so that you can use them. In particular, I got the intention to make a simple script where I get a screen portion and then I get the text out of the picture ready to be used in some text editor.
The code to grab a part of the screen is here (github repo). You have to click once for the top left corner of the part of the screen you want to get and then at the bottom down corner of the part of the screen you want to get.
In another post we will see how to use this code in other scripts.
Video example of how the code works
import pyscreenshot as ImageGrab
from pynput.mouse import Listener
def grab(x, y, w, h):
im = ImageGrab.grab(bbox=(x, y, w, h))
click1 = 0
x1 = 0
y1 = 0
def on_click(x, y, button, pressed):
global click1, x1, y1
if click1 == 0:
x1 = x
y1 = y
click1 = 1
grab(x1, y1, x, y)
print("Click once on top left and once on bottom right")
# with Listener(on_move=on_move, on_click=on_click, on_scroll=on_scroll) as listener:
with Listener(on_click=on_click) as listener:
Live coding explanation of the script to grab the screen
So, this should be the first step to make the text recognition script. With a couple of lines of code we will be able to do it. In fact the code to make it is already here, we just need to join it with this code to get the image.
Code to get the text out of a picture
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract'
As you can see it is really easy. You will need pytesseract. See the post.
See ya in a future post to put all together.Post written by A python enthusiast
Higlighted videosSpeech recognition game
Let's start using pygame to make games! Pretty fun assured.
- Python Snake game
- Snake version 1
- Snake version 2
- Snake Game version 3 with (experimental) music
- Making Snake game Part 1: the menu
- Python vs. Snake v. 1.8.3
- PySnake 1.8.7
- A bigger Snake Game
- PySnake gameover scene (devlog)
- Pysnake devlog 1.9.6
- Looking for fluid movements in snake
- PySnake 2.0.3 music and fly