Python Forum

Full Version: Image to Sound Python Project
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey Everyone, Can anyone tell me below code is correct or not? I am working on Image to Sound based python project and looking to source code. I have found below code from interviewbit and want to know your expert view, Is it right code or not and also I have checked this same code on github

prep:
Output:
pip install pytesseract pip install gTTS pip install pytesseract pip install gTTS
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 executing the code\n", bug)
        return
if __name__ == "__main__":
    image_to_sound("image.jpg")
    input()
(Jul-26-2021, 08:25 AM)ankitdixit Wrote: [ -> ]Hey Everyone, Can anyone tell me below code is correct or not?
It's really your task to test the code,i can give some advice on how i would test this code.
I would use virtual environment(build into Python) to make sure that there are no conflict with what installed before.
Virtual environment is like isolated new Python installation.

Can do a quick test Python 3.9.5.
# Make
G:\div_code
λ python -m venv image_sound_env

# Cd in
G:\div_code
λ cd image_sound_env\

# Activate
G:\div_code\image_sound_env
λ G:\div_code\image_sound_env\Scripts\activate

# Install
(image_sound_env) G:\div_code\image_sound_env
λ pip install pytesseract
Collecting pytesseract
.....  
Successfully installed Pillow-8.3.1 pytesseract-0.3.8

# Install
(image_sound_env) G:\div_code\image_sound_env
λ pip install gTTS pillow
....
Successfully installed certifi-2021.5.30 charset-normalizer-2.0.3 click-8.0.1 colorama-0.4.4
                              gTTS-2.2.3 idna-3.2 requests-2.26.0 six-1.16.0 urllib3-1.26.6
So at this point all installation went fine.
Find a image lyric.jpg
# image_sound.py
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 executing the code\n", bug)
        return
if __name__ == "__main__":
    image_to_sound("lyric.jpg")
    input()
Test that works.
Output:
(image_sound_env) G:\div_code\image_sound_env λ python image_sound.py YOUR GRACE ABOUNDS IN DEEPEST WATERS | YOUR SOVEREIGN HAND WILL BE MY GUIDE WHERE FEET MAY FAIL AND FEAR SURROUNDS ME ♀
So the OCR process shown over and generate of song.mp3 work fine for me.