Image grabber 1.0 with Python (final version)

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

# grabscreen.py

import pyscreenshot as ImageGrab
import os
from pynput.mouse import Listener
import sys

def grab(x, y, w, h):
    im = ImageGrab.grab(bbox=(x, y, w, h))
    save(im)


def save(im):
    im.save('im.png')
    os.startfile('im.png')

click1 = 0
x1 = 0
y1 = 0
def on_click(x, y, button, pressed):
    global click1, x1, y1
    
    if pressed:
        if click1 == 0:
            x1 = x
            y1 = y
            click1 = 1
        else:
            grab(x1, y1, x, y)
            listener.stop()
            sys.exit()

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:
    listener.join()
    listener.stop()
    sys.exit()

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

import pytesseract

pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract'
print(pytesseract.image_to_string(r'01.png'))

As you can see it is really easy. You will need pytesseract. See the post.

OCR: read a text from an image or a photo

See ya in a future post to put all together.


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.