Skip to content

Read the text in an image with Python

From time to time I need to read the text within an image.

Lets convert this meme to text:

Firstly – this is a top quality Dad Joke pun. If you don’t find this funny, I am truly wasted here.

So lets get the text. We can do that using the Python Tesseract library.

from PIL import Image
import pytesseract

# Open the image file
img = Image.open("/path/to/the/meme.jpg")

# Use pytesseract to do optical character recognition on the image
text = pytesseract.image_to_string(img)

print(text)

You will get this returned:

'@ darjeelingandcoke-deactivated20\n\nAn ancient Greek walks into his tailors shop\nwith a pair of torn pants.\n\n“Euripides?” says the tailor.\n\n“Yeah, Eumenides?” replies the man.\n\nBP, thiswillonlyhurtalittle\n\nThis is so awful. It must go on to infect\nothers.\n\x0c'

Which when we format it gives us this:

@ darjeelingandcoke-deactivated20

An ancient Greek walks into his tailor’s shop with a pair of torn pants.

“Euripides?” says the tailor.

“Yeah, Eumenides?” replies the man.

BP, thiswillonlyhurtalittle

This is so awful. It must go on to infect others.

Nice and Easy! Note that you get all the words in the meme – so you’ll probably want to remove the authors and the reply if you are just sending it as a joke.

Tags:

Leave a Reply