Extract Text From Image With Python


To extract text from image with python is pretty easy:

For some good reason one might want to extract texts from images, but the question is usually: how do I do that? , well.. if you happen to be among those that asked, Here comes the Calvary: Python is the answer.

Yes you heard right, to extract text from image with python is pretty easy. We are just going to use a few lines of code with just two python modules.

Without further ado, let’s dive into the codes already. We’ll begin by importing the relevant python modules for this program. Ever heard of Pytesseract before? Well if you’ve not, you might want to check the documentation here for a quick read-up.

To quickly read up the documentation about PIllow click here.

To install both modules is easy too, just use the normal pip install [module]. In case you re using linux distro like ubuntu and you got a “module not found error” on pytesseract, try installing tesseract-ocr first with sudo apt install tesseract-ocr ,then go ahead and install pytesseract with pip afterwards.

We are only going to use the 2 modules to convert an image to text here, to explore those modules further, check the module links I provided above. Now let’s import those modules at once:

import pytesseract as pt
from PIL import Image

Now let’s open the image with .Image.open from PIL module and use pytesseract to convert it to a string and also assign the string to a variable at once.

#importing the necessary modules
import pytesseract as pt
from PIL import Image

#Converting image to text
img = Image.open('/[image path]/image.jpg')
extracted = pt.image_to_string(img)

That’s all! You just got any readable text on your image. Easy Pizzy huh? Here we used a .jpg image, but you can even use a png as well, depending what your image extension is.

Meanwhile, you can now do whatever you want with your extracted text. Now that I have proved to you that to extract text from image with python is super easy, feel free to also check this image manipulation tutorial here and see if it could come in handy in your next python project.

If you happen to run into any puzzle or challenge on this tutorial, please don’t hesitate to write me, or leave a comment on comment section.

To automatically get the first hand information when the next post and subsequent ones gets published, subscribe to techswitch newsletter or join us on telegram.

Get an affordable and seamless python one on one training today from anywhere in the world, location is never a barrier, we have friendly learning tools to make your python programming training a worthwhile experience.

🤞 Don’t miss the tips!

We don’t spam! Read more in our privacy policy


Geoff is a python software engineer, web content specialist, tech private trainer and an IT virtual assistant.

Leave a Reply