Hello everyone,
Michael here, and today we’ve got another very special post! Yes dear readers, it’s time for another anniversary post-this year, Michael’s Programming Bytes turns 7!
So, how will I celebrate this anniversary? With a generic thank-you statement?
Not quite. I’ll take a cue from my 6th anniversary post (where I gave a surprise Python encryption lesson-https://michaelsprogrammingbytes.com/6-2/) and instead, show you something cool in Python to celebrate!
Let’s begin!
A little something called OCR
For this year’s anniversary celebration post, I’m going to a demo on something called OCR using Python.
What is OCR? It’s optical character recognition, which is a type of computer vision-which is a type of AI-that essentially reads any text in images and outputs the text it read to the program (to the best of its ability of course, as OCR like other types of AI can certainly make mistakes).
And now, for the OCR demo
What image might we be reading into the IDE?

Yes, we’ve got some red text and a lime-green image background (if you’ve read enough of this blog, you’ll notice that I’m quite fond of the lime-green-background-and-red-text image). Now, let’s see how well our OCR demo fares!
Package…installed
Before we start our fun little demo, let’s first install any necessary packages:
!pip install pytesseract
For this lesson, the only package we’ll need to install is the pytesseract package, which is a popular Python OCR library.
Imports…made
Next up, let’s import any modules we’ll need for the demo:
import pytesseract
import numpy as np
from PIL import Image
We’ll need three modules-pytesseract, numpy and PIL. As I just mentioned, pytesseract is the OCR library that we’ll use for this post. The PIL (Python Imaging Library/Pillow) library is like a companion to pytesseract, since even though PIL doesn’t do the actual OCR, it helps with all the important image preprocessing tasks (e.g. opening image into IDE, converting image to greyscale for optimal OCR). We’ve already explored the numpy library a while back, but I’ll explain why we’re using here shortly.
Image text…read
Last but not least, let’s read the image’s text:
testImage = '7 years.png'
testImageNP = np.array(Image.open(testImage))
testImageTEXT = pytesseract.image_to_string(testImageNP)
print(testImageTEXT)
Thank you for 7
wonderful years!
After storing the image with text as the testImage variable, we then use both the PIL Image module and the numpy module to open and preprocess the image.
- If you’re using a Google Colab notebook for this tutorial like I did, remember to save any files (images, CSVs) to the Colab runtime, as Colab can’t easily access your local drives.
Why do we need both modules? The Image module opens Why do we need both modules? The Image module reads the testImage into the IDE while numpy helps preprocess the image further by translating it to a series of pixels, which assists with further preprocessing tasks (such as greyscaling and text extraction).
After opening and preprocessing the image, we then use the pytesseract.image_to_string()-passing in the numpy version of the test image (testImageNP in this case)-method to save the extracted text as an object. Last but not least, we then print out the extracted text and see how accurate it is. In this case, we got Thank you for 7 \n wonderful years, which is exactly what the text on the image says (it even got the \n line break right!).
All in all, this simple OCR program seems to work like a charm! I might even do a whole Python OCR series where I test Python’s OCR capabilities for a bunch of interesting scenarios-but you’ll have to keep following & reading my blog to enjoy my cool coding content.
Since I used a Google Colab notebook to put this anniversary post together, here’s the link to that notebook in my GitHub-https://github.com/mfletcher2021/blogcode/blob/main/7.ipynb. The test image is also on my GitHub, but it is attached separately (the image is called 7 years.png).
One more thing…
…I know I say this after every post, but thank you for following along my coding how-to journey for the last 7 years and 187 posts (pushing the 200-mark)! Hopefully you’ve picked up some new skills, or at least some juicy knowledge to impress on bar trivia night (I mean, I wouldn’t be disappointed in that), about the wonderful world of coding-or any other thing I’ve happened to reference on this blog over the last 7 years.
Also, if you’re wondering how long I’ll keep going, have no fear, I’ve got content for years!
Keep calm and code on!
Michael
Also, let me leave you with a picture of these two furry kittes-Simba and Marbles on Christmas Day 2017-they certainly saw me write some of my earliest content:
