In this tutorial, I will guide to building a Python program capable of converting an image to sound.
Throughout the tutorial, we will learn concepts of Optical character recognition (OCR) and Speech synthesis and later combining them to create a single working program.
$ pip install Pillow $ pip install gTTS $ pip install pytesseract
Also in order pytesseract to work you have to install Google’s Tesseract-OCR Engine on your machine.
To install Tesseract Engine, CLICK HERE to get full instruction on installation with respect to your operating system
Now after Everything is installed, let’s start building our program
On your project folder, you should have a sample image containing text on it which we could use to test our program
. ├── app.py └── image.jpg 0 directories, 2 files
Our project will be divided into two main parts
- Converting the image to text(Optical character Recognition)
- Converting Generated Text to speech (Speech Synthesis)
Converting Image to Text
At this stage, we use Python Library pytesseract to perform Optical character recognition which can be done in only one line of code.
But just before we begin to perform Optical Character recognition on our image, we need a way to load the image to the required format,
On this, we gonna use the Pillow library, Let's see how to convert image to text using pytesseract and pillow as shown in the example
Example of Usage
>>> from PIL import Image >>> from pytesseract import image_to_string >>> text = image_to_string(Image.open('image.jpg')) 'JOBS FILL\nYﬂUR POCKET.\nADVENTURES\nFILL YOUR\nLIFE.'
That’s how you can easily perform OCR in just 1 line of code, now let’s go see how can we convert it to speech using gTTS
Converting Generated Text to speech
There various ways you convert to speech to text in Python, in case you wanna review them all you can CLICK HERE
In this tutorial, We are going to use google text to speech to convert our decoded text into sound.
The Syntax to performing text to speech is very simple you can also do it just one line of code as shown in the example below
>>> from gtts import gTTS >>> gTTS('Coding is awesome trust me').save('sound.mp3')
I made the below simple program using the knowledge we just learned above with the addition of a cleaner function to remove \n in a generated text to make it easily convertible to sound.
from PIL import Image from gtts import gTTS from pytesseract import image_to_string def image_to_sound(path_to_image): """ Function for converting an image to sound """ try: loaded_image = Image.open(path_to_image) decoded_text = image_to_string(loaded_image) cleaned_text = " ".join(decoded_text.split("\n")) print(cleaned_text) sound = gTTS(cleaned_text, lang="en") sound.save("sound.mp3") return True except Exception as bug: print("The bug thrown while excuting the code\n", bug) return if __name__ == "__main__": image_to_sound("image.jpg") input()
When you run the above code, it will open our sample image, perform optical character recognition, clean generated text by removing \n, convert into sound by using gTTS
In case you find it interesting, don't be shy share it with your fellow friends on twitter and other social media.