Python Forum
Image to Sound Python Project
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Image to Sound Python Project
#1
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()
Larz60+ write Jul-26-2021, 10:29 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Upload image to Instagram using python/selenium using image URL failed, need help greenpine 5 5,340 Feb-22-2022, 09:42 PM
Last Post: snippsat
  Real Time Audio Processing with Python Sound-Device not working Slartybartfast 2 3,882 Mar-14-2021, 07:20 PM
Last Post: Slartybartfast
  Play fixed frequency sound in python 3 jpezz 2 2,724 Feb-07-2021, 08:21 PM
Last Post: jpezz
  Python 3.7, Windows 10, pyinstaller, winsound, no sound in executable kmarien 3 3,589 Nov-30-2020, 04:10 PM
Last Post: buran
  PIL Image / python-resize-image AttributeError sallyjc81 1 4,905 Aug-02-2020, 12:06 AM
Last Post: scidam
  Change Windows Sound Output Device with Python delfar 1 10,231 Sep-15-2017, 12:11 AM
Last Post: Larz60+
  How to change the sound volume with python ? Lightningwalrus 5 57,873 Sep-30-2016, 08:21 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020