How Do I Extract Text from a Screenshot?

Post by
Andrew Pierno
How Do I Extract Text from a Screenshot?

In several of our earlier posts, we’ve outlined how you can take screenshots automatically with everything from tools to PHP, Python, Ruby, JavaScript, and more.  

The thing is, once you’ve taken screenshots, you’ll often need to extract the text from them. For example, if you want to track your competition’s pricing. The problem is that you can’t just copy & paste. So in this post, we’ll show you how to extract text from screenshots using Python and a simpler method.

How to Extract Text From a Screenshot

To extract text from screenshots, you’ll need to use Optical Character Recognition (OCR). It uses computer vision and pattern recognition algorithms and is generally trained with sample data. Combined, this allows OCR tools to identify and extract text from digital images like screenshots. 

Different Ways to Use OCR

There are several ways in which you can use OCR to extract text from images. These include:

  • Online tools and websites
  • Google Docs’ built-in OCR functionality
  • Installable applications.
  • Browser extensions

The problem with most of these tools is that they typically don’t provide simple ways to take screenshots automatically or in bulk. In these cases, you’ll probably need another solution.

Extracting Text From Screenshots Using Python

If you’ve read our earlier posts, you’ll know that one of the best ways to take screenshots automatically is to do it programmatically. To extract text from images using Python, you’ll use the Python-tesseract or pytesseract OCR library. 

To use the library, you’ll first import it into your Python code. You’ll also import the Python Imaging Library (PIL).

Once you’ve imported the required libraries, you’ll define the paths to the pytesseract executable and the image you want to use. Here, it’s important to remember that you should install the tesseract executable before you start.

You’ll then open the screenshot image and store it as an image object so you can use it with the pytesseract library. 

To extract the text from the screenshot, you’ll need to tell the pytesseract library where the tesseract executable is, as it will do all the work behind the scenes. 

Here, you’ll use this code:

You’ll then pass the image object you created earlier to the pytesseract library and use the image_to_string function to extract the text from the image. 

Finally, you’ll display the extracted text.

By making small changes to this code, you can take screenshots in bulk, take a series of screenshots automatically, or even export the text to a text file. 

Is There a Simpler Way to Extract Text from a Screenshot? 

We’re glad you asked! If you’re taking programmatic screenshots with ScreenshotAPI, extracting the text is as simple as ticking the right box.

If you’re ready to do less tedious work, head over to ScreenshotAPI and get your free API key today!