cipheron

Untitled

Apr 19th, 2024 (edited)
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import *
  3. from tkinter import filedialog, messagebox # Added messagebox
  4. from tkinter.ttk import Combobox, Style
  5. import pyttsx3
  6. import os
  7. import pytesseract
  8. from PIL import Image, ImageTk
  9.  
  10. root = Tk()
  11. root.title("Text to Speech")
  12. root.geometry("900x500+100+100")
  13. root.resizable(False, False)
  14. root.configure(bg="#1A3B59")
  15.  
  16. # Functions
  17. engine = pyttsx3.init()
  18.  
  19. def setvoice(gender):
  20. voices = engine.getProperty('voices')
  21. if (gender == 'David'):
  22. engine.setProperty('voice', voices[0].id)
  23. elif(gender=='Tony'):
  24. engine.setProperty('voice', voices[1].id)
  25. elif(gender=='Sam'):
  26. engine.setProperty('voice', voices[2].id)
  27. elif(gender=='Meera'):
  28. engine.setProperty('voice', voices[3].id)
  29. else:
  30. engine.setProperty('voice', voices[4].id)
  31.  
  32. def speaknow():
  33. text = text_area.get(1.0, END).strip()
  34. if not text:
  35. messagebox.showwarning("No Text", "Please enter some text before speaking.") # Show warning message
  36. return
  37. gender = gender_combobox.get()
  38. speed = speed_combobox.get()
  39.  
  40. if (speed == "Fast"):
  41. engine.setProperty('rate', 250)
  42. elif (speed == "Normal"):
  43. engine.setProperty('rate', 150)
  44. else:
  45. engine.setProperty('rate', 60)
  46.  
  47. setvoice(gender)
  48.  
  49. def run_pyttsx3(text):
  50. engine.say(text)
  51. engine.runAndWait()
  52.  
  53. import threading
  54. threading.Thread(
  55. target=run_pyttsx3, args=(text,), daemon=True
  56. ).start()
  57.  
  58.  
  59. def download():
  60. text = text_area.get(1.0, END).strip()
  61. if not text:
  62. messagebox.showwarning("No Text", "Please enter some text before downloading.") # Show warning message
  63. return
  64. gender = gender_combobox.get()
  65. speed = speed_combobox.get()
  66.  
  67. if (speed == "Fast"):
  68. engine.setProperty('rate', 250)
  69. elif (speed == "Normal"):
  70. engine.setProperty('rate', 150)
  71. else:
  72. engine.setProperty('rate', 60)
  73.  
  74. setvoice(gender)
  75.  
  76. file_path = filedialog.asksaveasfilename(defaultextension=file_extension, filetypes=file_types)
  77. if file_path:
  78. engine.save_to_file(text, file_path)
  79. # engine.runAndWait()
  80.  
  81. def open_image():
  82. file_path = filedialog.askopenfilename(filetypes=[("Image Files", "*.png *.jpg *.jpeg")])
  83. if file_path:
  84. img = Image.open(file_path)
  85. text = pytesseract.image_to_string(img)
  86. text_area.insert(END, text)
  87.  
  88. # File types for saving
  89. file_types = [('MP3 Files', '*.mp3'), ('WAV Files', '*.wav'), ('OGG Files', '*.ogg')]
  90. file_extension = file_types[0][1][1:] # Default file extension is MP3
  91.  
  92. # Icon
  93. image_icon = PhotoImage(file="speak.png")
  94. root.iconphoto(False, image_icon)
  95.  
  96. # Top frame
  97. Top_frame = Frame(root, bg="#FFFFFF", width=900, height=100)
  98. Top_frame.place(x=0, y=0)
  99. Logo = PhotoImage(file="speaker_logo.png")
  100. Label(Top_frame, image=Logo, bg="#FFFFFF").place(x=10, y=5)
  101. Label(Top_frame, text="TEXT TO SPEECH", font="Arial 20 bold", bg="#FFFFFF", fg="#1A3B59").place(x=100, y=30)
  102.  
  103. # Text area
  104. text_area = Text(root, font="Roboto 16", bg="#EFF4F4", relief=GROOVE, wrap=WORD, borderwidth=2, padx=10, pady=10)
  105. text_area.place(x=10, y=150, width=500, height=250)
  106.  
  107. # Rounded corners for the text area
  108. radius = 20
  109. text_area.config(highlightbackground="#BFBFBF", highlightthickness=1, highlightcolor="#BFBFBF")
  110. text_area.bind("<Enter>", lambda e: text_area.config(highlightbackground="#1A3B59", highlightthickness=2))
  111. text_area.bind("<Leave>", lambda e: text_area.config(highlightbackground="#BFBFBF", highlightthickness=1))
  112.  
  113. # Label for Voices
  114. Label(root, text="VOICE", font="Arial 14 bold", bg="#1A3B59", fg="#FFFFFF").place(x=580, y=160)
  115.  
  116. # Label for speed
  117. Label(root, text="SPEED", font="Arial 14 bold", bg="#1A3B59", fg="#FFFFFF").place(x=760, y=160)
  118.  
  119. # Custom style for dropdown boxes
  120. style = Style()
  121. style.theme_create("CustomStyle", settings={
  122. "TCombobox": {
  123. "configure": {
  124. "padding": 5,
  125. "background": "#F5F5F5",
  126. "foreground": "#333333",
  127. "fieldbackground": "#F5F5F5",
  128. "bordercolor": "#BFBFBF",
  129. "selectbackground": "#1A3B59",
  130. "selectforeground": "#FFFFFF",
  131. "arrowcolor": "#1A3B59",
  132. "borderwidth": 1, # Add border width
  133. "relief": "solid", # Add relief
  134. "border-radius": 5 # Add border radius
  135. }
  136. },
  137. "TMenubutton": {
  138. "configure": {"padding": 5}
  139. },
  140. "TButton": { # Style for buttons
  141. "configure": {
  142. "borderwidth": 1, # Add border width
  143. "relief": "solid", # Add relief
  144. "border-radius": 5 # Add border radius
  145. }
  146. }
  147. })
  148. style.theme_use("CustomStyle")
  149.  
  150. # Gender combobox
  151. gender_combobox = Combobox(root, values=['David', 'Tony','Sam','Meera','Zara'], font="Arial 12", state="readonly", width=10)
  152. gender_combobox.place(x=550, y=200)
  153. gender_combobox.set('David')
  154.  
  155. # Speed combobox
  156. speed_combobox = Combobox(root, values=['Fast', 'Normal', 'Slow'], font="Arial 12", state="readonly", width=10)
  157. speed_combobox.place(x=730, y=200)
  158. speed_combobox.set('Normal')
  159.  
  160.  
  161. # Image Button
  162. imageicon3 = PhotoImage(file="image.png")
  163. image_btn = Button(root, text="Add\nImage", compound=LEFT, image=imageicon3, width=120, font="arial 13 bold", command=open_image)
  164. image_btn.place(x=550, y=260) # Adjust the coordinates as needed
  165.  
  166. # Speak Button
  167. imageicon = PhotoImage(file="speaking.png")
  168. btn = Button(root, text="Speak", compound=LEFT, image=imageicon, width=120, font="arial 14 bold", command=speaknow)
  169. btn.place(x=550, y=360) # Adjust the coordinates as needed
  170.  
  171. # Save Button
  172. imageicon2 = PhotoImage(file="save-file.png")
  173. save = Button(root, text="Save", compound=LEFT, image=imageicon2, width=120, bg="#EFF4F4", font="arial 14 bold", command=download)
  174. save.place(x=730, y=360) # Adjust the coordinates as needed
  175.  
  176. root.mainloop()
  177.  
Advertisement
Add Comment
Please, Sign In to add comment