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
Pygame cheatsheets Videos about this map editor
New free game: Crystal of time
How to make a map editor 1
How to make a map editor 2
How to make a map editor 3
Map editor 1.5
Map editor 1.6
How to make a videogame map editor with Python - MEP v. 2.1
Map editor for 2d platform game in Python with Pygame v.3.0
How to save a list with pickle with python
Pygame Map Editor 4.0 for Crystals of time
Newest branch (to clone it with git)
git clone --branch cotb2 https://github.com/formazione/timecrystals.git