# How to Read Text From an Image with Python

If you want to read text from an image with a simple Python script, this tutorial is for you. Thanks to the work of many great people over the last few decades, you can read the text from an image with a few lines of code. Really! Let’s jump in.

![How to read text from an image with Python](https://www.jeremymorgan.com/images/tutorials/python-tutorials/read-text-from-image/read-text-from-image-00.webp align="left")

## What is OCR? Tesseract?

Optical Character Recognition, or OCR has [been around for a long time](https://www.docsumo.com/blog/optical-character-recognition-history). It's a technique that “reads” different types of documents into editable and searchable text. It works by recognizing characters in the image and converting them into machine-readable text. It’s a lot of magic, but it works well.

[Tesseract](https://github.com/tesseract-ocr/tesseract) is an open-source OCR engine developed by Google. It is highly accurate and supports multiple languages. This library will do all the heavy lifting for us. We’ll use it in this tutorial to quickly read the text in some images.

### Step 1: Set up your Python Environment

First, you’ll need to make sure Python is installed. We’re going to create a virtual environment.

* [How to install Python and set up a virtual environment in Windows](https://www.pythonhelp.org/learn/introduction/setting-up-development-environment-windows/)
    
* [How to set up your Python environment on a Mac](https://www.pythonhelp.org/learn/introduction/setting-up-development-environment-mac/)
    
* [How to setup Python environment in Linux](https://www.pythonhelp.org/learn/introduction/setting-up-development-environment-linux/)
    

I’m using Linux, so I’ll create a directory named `textreader` and type in

```plaintext
python -m venv textreader
```

Then

```plaintext
source textreader/bin/activate
```

### Step 2: Install the Required Libraries

First, we’ll need to install Tesseract on your system. Here are the [instructions to install Tesseract](https://tesseract-ocr.github.io/tessdoc/Installation.html) on your chosen operating system.

Make sure Tesseract is installed by typing:

```plaintext
tesseract -v
```

and you should see output that looks like this:

![How to read text from an image with Python](https://www.jeremymorgan.com/images/tutorials/python-tutorials/read-text-from-image/read-text-from-image-01.webp align="left")

Then, we’ll install a couple of Python libraries.

[Pytesseract](https://pypi.org/project/pytesseract/) is a Python library that is a wrapper for the Tesseract OCR engine. This makes it easy to use in Python applications. We’ll install that and Pillow.

[Pillow](https://pypi.org/project/Pillow/) is the Python Image Library. It’s used for image processing and manipulation. It’s used to pre-process images before applying OCR techniques. It does things like image thresholding and other steps to the image to enhance the accuracy of the reading.

Next, we’ll install Pytesseract and Pillow together for our first application:

```plaintext
pip install pytesseract
pip install pillow
```

Your output should look something like this:

![How to read text from an image with Python](https://www.jeremymorgan.com/images/tutorials/python-tutorials/read-text-from-image/read-text-from-image-02.webp align="left")

In some cases, like above, it may say the requirement is already satisfied for Pillow.

And we’re ready to go.

### Step 3: Select your Image

To start, I’m going to choose something easy. I’ll use a screenshot from my website. This will be clear, easy-to-read text that should work great.

![How to read text from an image with Python](https://www.jeremymorgan.com/images/tutorials/python-tutorials/read-text-from-image/read-text-from-image-03.webp align="left")

I’ll save that as image-1.jpg in my folder.

### Step 4: Write the Script

Now, we’re ready to build our Python script to read the text from that image and output it to the screen.

First, we’ll import the libraries:

```python
import pytesseract
from PIL import Image
```

Then open the image:

```python
image = Image.open('image-1.jpg')
```

And then, we’ll use Tesseract to convert the text in the image to a string. Didn’t I say this library does *all* the heavy lifting for us?

```python
text = pytesseract.image_to_string(image)
```

Finally, we’ll print it out:

```python
print(text)
```

Let’s run it and see what it looks like.

### Step 5: Watch the Magic Happen

We run our script and get this:

![How to read text from an image with Python](https://www.jeremymorgan.com/images/tutorials/python-tutorials/read-text-from-image/read-text-from-image-04.webp align="left")

Awesome! So it’s not perfect, but it’s pretty darn good. You can read the text from the image we sent, and it’s somewhat formatted the way it is in the image. That’s awesome!

Congrats! You can now read the text from images in Python. Next, we’ll look at some more advanced stuff.

### Learning the Limitations

In our first example, we had a very clear image. The text is formatted and crisp in that image, so it’s easy to read. Let’s step it up a bit.

I picked a more challenging image, one from [Pexels](https://www.pexels.com/photo/welcome-to-our-home-print-brown-wooden-wall-decor-163046/), that isn’t quite so easy.

![How to read text from an image with Python](https://www.jeremymorgan.com/images/tutorials/python-tutorials/read-text-from-image/read-text-from-image-05.webp align="left")

Let’s see what the output is when reading this image:

![How to read text from an image with Python](https://www.jeremymorgan.com/images/tutorials/python-tutorials/read-text-from-image/read-text-from-image-06.webp align="left")

Oof. Nothing. I included this because it’s important to know the limitations of this process. Unusual fonts and different angles will affect how well this works. There isn’t much we can do to read this image without some extensive work.

### Conclusion

In this tutorial, we learned how to use Tesseract to read text from an image and put it into a machine-readable form. We can read many other things with OCR, and we’ll deep dive into some of this stuff in future articles.

Feel free to play around with this and see what you can come up with! In a future tutorial, we’ll use OpenCV to refine things and do more pre-processing of the images we’ll read from. It will be fun.

Bookmark this blog and come back for more cool Python tutorials.

Questions? Comments? [Yell at me!](https://x.com/intent/follow?screen_name=JeremyCMorgan)

**If you're into Computer Vision and other cool AI tech, you should subscribe to my** [**AI Architect Newsletter**](https://mailchi.mp/4961a415a64f/learn-ai-newsletter) **to keep up with the latest stuff!**
