Extract Text from Images using Python ( OCR )

Sam Hagin
Do It With Code
Published in
2 min readMay 12, 2023

If you’d prefer a video instead, watch the tutorial below:

In this tutorial, we will convert an image to text using Python. This process is called OCR which stands for Optical Character Recognition.

We’re going to extract the text from this image. We’re using PyCharm but feel free to use any text editor or IDE of your choice. Right-click and click on New then to create a new file. We’re going to name it text-from-image.py.

Once the file has been created, let’s install the Python packages we need. Click on terminal at the bottom then type pip install pillow pytesseract. This will install both Python packages. After this is completed, we need to import these packages.

from PIL import Image
import pytesseract

We can then create a variable called image which reads and stores the image. The image is in the same directory as our Python file so we just need to specify the image name.

image = Image.open('image-to-text.png')

However, if the image is located in another directory you will have to specify the full path. For instance, an image in Downloads on a Mac, will have the path /Users/your_username/Downloads/image-to-text.png and then the name of the file. If on Windows, the directory will be something like this C:\\Users\Administrator\\Downloads\\image-to-text.png. Note, that instead of forward slashes, you use double back slashes on Windows.

We then create another variable called text which will hold the text from the image.

text = pytesseract.image_to_string( image )

We then print out the text from the image on the next line.

print( text )

Right-click then click on Run. The text is then displayed on the console.

The full code and the image text is extracted from are below

from PIL import Image
import pytesseract

image = Image.open('image-to-text.png')
text = pytesseract.image_to_string( image )
print( text )

For more tutorials like this, visit doitwithcode.com

--

--

Sam Hagin
Do It With Code

Software Developer, Data Engineer, Automation Freak, 💙 WordPress http://doitwithcode.com