How to Read Text From an Image with Python
Tags: python, tutorials, programming, beginners
Published: 2023-10-23 16:39:22

---
title: How to Read Text From an Image with Python
published: true
description: Want to read text from an image? Of course you do, and it's super easy with Python and Tesseract. Here's a full tutorial to show you how.
tags: python, tutorials, programming, beginners
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/02pr0bcjeq5kxo68rypt.jpg
canonical_url: https://www.jeremymorgan.com/tutorials/python-tutorials/read-text-from-image/
# Use a ratio of 100:42 for best results.
# published_at: 2023-10-23 16:29 +0000
---

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][00]

## 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). Its 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 

```
python -m venv textreader
```

Then

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

### Step 2: Install the Required Libraries

First, we'll need to install Tesseract on your system. Here's 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: 

```
tesseract -v
```
and you should see output that looks like this:

![How to read text from an image with Python][01]

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 on the image to enhance the accuracy of the reading. 

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

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

Your output should look something like this: 

![How to read text from an image with Python][02]

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 out, 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][03]

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][04]

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][05]

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

![How to read text from an image with Python][06]

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!](http://bit.ly/JeremyCMorgan)

[00]: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3gze18sxzhbfjtis1gft.png
[01]: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9giodok8hk1ll7qh5j0e.png
[02]: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d5y6tv1daq8pe7it692z.png
[03]: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/98xkx5eksi2o32oftllx.png
[04]: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4wth7t5j1wsr94lby4a2.png
[05]: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rzcrcq9rrprjdq2d2ik3.png
[06]: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jo72ia901muewi06lqn3.png