Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor image-to-sound script for readability and batch processing #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 47 additions & 14 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,58 @@
from PIL import Image
from gtts import gTTS
from pytesseract import image_to_string
import os


def image_to_sound(path_to_image):
def image_to_sound(image_path, output_sound_path="sound.mp3"):
"""
Function for converting an image to sound
Converts an image to speech audio by extracting text from the image
and converting it to sound.

:param image_path: Path to the image file.
:param output_sound_path: Path where the sound file will be saved.
:return: True if successful, False otherwise.
"""
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
# Open and process the image
with Image.open(image_path) as img:
decoded_text = image_to_string(img)
cleaned_text = " ".join(decoded_text.splitlines())

if cleaned_text.strip():
print(f"Extracted text: {cleaned_text}")
sound = gTTS(cleaned_text, lang="en")
sound.save(output_sound_path)
print(f"Sound saved to {output_sound_path}")
return True
else:
print("No text found in the image.")
return False

except Exception as error:
print(f"An error occurred: {error}")
return False


def process_images_in_directory(directory_path):
"""
Processes all image files in a directory, converting each to a sound file.

:param directory_path: Path to the directory containing image files.
"""
for filename in os.listdir(directory_path):
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')):
image_path = os.path.join(directory_path, filename)
sound_filename = os.path.splitext(filename)[0] + ".mp3"
sound_path = os.path.join(directory_path, sound_filename)
success = image_to_sound(image_path, sound_path)
if success:
print(f"Processed {filename} successfully.\n")
else:
print(f"Failed to process {filename}.\n")


if __name__ == "__main__":
image_to_sound("image.jpg")
input()
directory = "images" # Change this to your directory path
process_images_in_directory(directory)
input("Press Enter to exit...")
1 change: 1 addition & 0 deletions visaa_prep/mrecw.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kadvipsf[agrhj gibr asjobdspieag
7 changes: 7 additions & 0 deletions visaa_prep/ticket_fever.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
t = int(input())
for _ in range(0,t):
n,m = map(int,(input().split()))
if n>m:
print(n-m)
else:
print(0)