Python | Convert image to text and then to speech
Our goal is to convert a given text image into a string of text, saving it to a file and to hear what is written in the image through audio.
For this, we need to import some Libraries
- Pytesseract(Python-tesseract) : It is an optical character recognition (OCR) tool for python sponsored by google.
- pyttsx3 : It is an offline cross-platform Text-to-Speech library
- Python Imaging Library (PIL) : It adds image processing capabilities to your Python interpreter
- Googletrans : It is a free python library that implements the Google Translate API.
Examples:
Input : We Have an image with some text
Output: THE TEXT FROM THE IMAGE IS EXTRACTED AND A VOICE WILL SPEAK THE TEXT
This is the first line of
this text example.
This is the second line
of the same text.
Translated(src=en, dest=de, text=Dies ist die erste Zeile von
Dieses Textbeispiel.
Dies ist die zweite Zeile
desselben Textes., pronunciation=None, extra_data="{'translat..."
Code : Python code to convert text to speech
import pytesseract
from PIL import Image
import pyttsx3
from googletrans import Translator
img = Image. open ( 'text1.png' )
print (img)
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract.exe'
result = pytesseract.image_to_string(img)
with open ( 'abc.txt' ,mode = 'w' ) as file :
file .write(result)
print (result)
p = Translator()
k = p.translate(result,dest = 'german' )
print (k)
engine = pyttsx3.init()
engine.say(k)
engine.runAndWait()
|
NOTE : We can convert the text into any desired language. For Example Japanese, Russian, Hindi. But the only condition is that the googletrans should recognize the destination language. Also, pyttsx3 will speak only the languages which are recognized by it.
Last Updated :
09 Sep, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...