Predictive Hacks

How to extract Text from Images

At this post we will give a brief overview of how we can extract Text from Images using the Python libraries Pillow and pytesseract. At the beginning we will provide a quick tutorial about the Pillow library.

Before you start running the reproducible examples below I would suggest to load the following libraries:

import PIL
from PIL import Image
import requests
import pytesseract
from io import BytesIO
from PIL import ImageFilter
from PIL import ImageEnhance
from IPython.display import display


Pillow – Read and modify images

Q: How to read images from URL and from the file?

# read images from URL
url = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS-Qx3hARh_hbR35nhFbzdMq1oUdErJ-UQgU2RhxWUsY-XXemGR"
response = requests.get(url)
img = Image.open(BytesIO(response.content))

# save in your PC
img.save("mysavedimage.png")

# load the image from the file path
img=Image.open("mysavedimage.png")

display(img)

Q: How to get the size of the image and how to resize it?

# get the image of the size
img.size
# it prints (364, 138)

# resize the image 
img.resize((300,300))


# resize image by keeping its ascpect ratio
# https://stackoverflow.com/questions/273946/how-do-i-resize-an-image-using-pil-and-maintain-its-aspect-ratio
basewidth = 300
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
imgresized = img.resize((basewidth,hsize), Image.ANTIALIAS)
imgresized.save('resized.jpg') 

Q: How to crop images?

display(img.crop((0,0,200,100)))

Q: How to add filters and how to convert it to gray scale and binary?

# this is about brightness
enhancer=ImageEnhance.Brightness(img)
enhancer.enhance(4/10)

# gray scale
img.convert('L')

# binary
img.convert('1')

We represent the gray-scale image



Q: How to create a canvas of images (optional)?

# Lets write a little loop to generate ten images of different brightness. First we need the Brightness
# object with our image
enhancer=ImageEnhance.Brightness(img)
images=[]
for i in range(0, 10):
    # We'll divide i by ten to get the decimal value we want, and append it to the images list
    # we actually call the brightness routine by calling the enhance() function. Remember, you can dig into
    # details of this using the help() function, or by consulting web docs
    images.append(enhancer.enhance(i/10))

first_image = img.copy()
#Ok, that's a nice proof of concept. But it's a little tough to see. Lets instead change this to a three
# by three grid of values. First thing we should do is make our canvas, and we'll make it 3 times the
# width of our image and 3 times the height of our image - a nine image square
contact_sheet=PIL.Image.new(first_image.mode, (first_image.width*3,first_image.height*3))
# Now we want to iterate over our images and place them into this grid. Remember that in PIL we manage the
# location of where we refer to as an image in the upper right hand corner, so this will be 0,0. Lets use
# one variable for the X dimension, and one for the Y dimension.
x=0
y=0

# Now, lets iterate over our images. Except, we don't want to both with the first one, because it is
# just solid black. Instead we want to just deal with the images after the first one, and that should
# give us nine in total
for img in images[1:]:
    # Lets paste the current image into the contact sheet
    contact_sheet.paste(img, (x, y) )
    # Now we update our X position. If it is going to be the width of the image, then we set it to 0
    # and update Y as well to point to the next "line" of the contact sheet.
    if x+first_image.width == contact_sheet.width:
        x=0
        y=y+first_image.height
    else:
        x=x+first_image.width

# Now lets resize the contact sheet. We'll just make it half the size by dividing it by two. And, because
# the resize function needs to take round numbers, we need to convert our divisions from floating point
# numbers into integers using the int() function.
contact_sheet = contact_sheet.resize((int(contact_sheet.width/2),int(contact_sheet.height/2) ))
# Now lets display that composite image
display(contact_sheet)

pytesseract – Extract Text from Image

Below we will give some examples of how we can get the text of the image. Let’s continue with our example:

text = pytesseract.image_to_string(img) 
print(text)

You are wondering why the print command does not return anything!!! Most likely because the analysis of the image is not good, the size is relative small and also there is a another color behind. For that reason it is a always a good technique to consider the gray-scale. For instance:

text = pytesseract.image_to_string(img.convert('L')) 
print(text)
‘#LiveMore with our
fastest ever 4Gnetwork

SuperNet

Another example with this image:

# another example
URL = "https://www.timeanddate.com/scripts/cityog.php?title=Current%20Local%20Time%20in&city=New%20York&state=New%20York&country=USA&image=new-york1"
response = requests.get(URL)
img = Image.open(BytesIO(response.content))

text = pytesseract.image_to_string(img.convert('L')) 
print(text)
Current Local Time in

New York

New York USA
# in this case it works also without the gray scale
text = pytesseract.image_to_string(img) 
print(text)
Current Local Time in

New York

New York USA

Let’s do another test by testing “ourselves” meaning to take a screenshot from the blog and see if we can extract the text.

# Try with gray scale
img=Image.open("screenshot.PNG")
text = pytesseract.image_to_string(img.convert('L')) 
print(text)
At this post we will give a brief overview of how we can extract Text from Images using the

Python libraries Pillow and pytesseract. At the beginning we will provide a quick tutorial about
the Pillow library.

Before you start running the reproducible examples below | would suggest to load the
following libraries:

import PIL

from PIL import Image
import requests
import pytesseract
# Try as is
img=Image.open("screenshot.PNG")
text = pytesseract.image_to_string(img) 
print(text)
ni nai

 

How To Extract Text From Images

George Pipis August 24, 2019 4min read

 

At this post we will give a brief overview of how we can extract Text from Images using the
Python libraries Pillow and pytesseract. At the beginning we will provide a quick tutorial about
the Pillow library.

Before you start running the reproducible examples below | would suggest to load the
following libraries:

import PIL

from PIL import Image
import requests
import pytesseract

As we can see we managed to get most of the text but we failed to get the top headers, in addition, the second command captured the Header too but it added some wrong text like “ni nai”. Finally, we did not manage to get the text from the image within the image :-). The pytesseract does not work always perfectly, but we can improve its outcome by applying some more advanced techniques to the images. We can discuss about it in another post!

Note: It is likely for windows users, to face some issues with the pytesseract library since you may need to add the path manually. More details at the stackoverflow.

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

4 thoughts on “How to extract Text from Images”

Leave a Comment

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.

Python

Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s