|
| 1 | +import pytesseract |
| 2 | +from PIL import Image, ImageGrab |
| 3 | +import tkinter as tk |
| 4 | +from tkinter import filedialog, messagebox, Text, Scrollbar |
| 5 | + |
| 6 | +def extract_text_from_image(img): #Extracts Text from Image using Pytesseract |
| 7 | + try: |
| 8 | + text = pytesseract.image_to_string(img) |
| 9 | + return text |
| 10 | + except Exception as e: |
| 11 | + print(e) |
| 12 | + return None |
| 13 | + |
| 14 | +def select_image_file(): # Upload a Image file |
| 15 | + img_path = filedialog.askopenfilename(title="Select Screenshot", filetypes=[("Image Files", "*.png *.jpg *.jpeg *.bmp")]) |
| 16 | + if img_path: |
| 17 | + img = Image.open(img_path) |
| 18 | + display_extracted_text(img) |
| 19 | + |
| 20 | +def paste_image_from_clipboard(): # Take partial screenshot (Windows + shift + S) it will be saved in clipboard. |
| 21 | + try: |
| 22 | + img = ImageGrab.grabclipboard() |
| 23 | + if isinstance(img, Image.Image): |
| 24 | + display_extracted_text(img) |
| 25 | + else: |
| 26 | + messagebox.showwarning("Error", "No image found in the clipboard.") |
| 27 | + except Exception as e: |
| 28 | + messagebox.showerror("Error", f"Error accessing clipboard: {e}") |
| 29 | + |
| 30 | +def capture_screenshot(): # Takes entire screen screenshot |
| 31 | + try: |
| 32 | + img = ImageGrab.grab() |
| 33 | + display_extracted_text(img) |
| 34 | + except Exception as e: |
| 35 | + messagebox.showerror("Error", f"Error capturing screenshot: {e}") |
| 36 | + |
| 37 | +def display_extracted_text(img): # Displaying extracted text from pytesseract |
| 38 | + text = extract_text_from_image(img) |
| 39 | + if text: |
| 40 | + text_display.delete(1.0, tk.END) |
| 41 | + text_display.insert(tk.END, text) |
| 42 | + else: |
| 43 | + messagebox.showwarning("Error", "No text could be extracted or an error occurred.") |
| 44 | + |
| 45 | +def main(): |
| 46 | + global text_display |
| 47 | + |
| 48 | + root = tk.Tk() |
| 49 | + root.title("Text Extractor with Python") |
| 50 | + |
| 51 | + text_frame = tk.Frame(root) |
| 52 | + text_frame.pack(pady=10) |
| 53 | + |
| 54 | + scrollbar = Scrollbar(text_frame) |
| 55 | + scrollbar.pack(side=tk.RIGHT, fill=tk.Y) |
| 56 | + |
| 57 | + text_display = Text(text_frame, wrap=tk.WORD, yscrollcommand=scrollbar.set, height=15, width=60) |
| 58 | + text_display.pack() |
| 59 | + |
| 60 | + scrollbar.config(command=text_display.yview) |
| 61 | + |
| 62 | + btn_select_file = tk.Button(root, text="Select Image File", command=select_image_file, width=30) |
| 63 | + btn_select_file.pack(pady=10) |
| 64 | + |
| 65 | + btn_paste_clipboard = tk.Button(root, text="Paste Image from Clipboard", command=paste_image_from_clipboard, width=30) |
| 66 | + btn_paste_clipboard.pack(pady=10) |
| 67 | + |
| 68 | + btn_capture_screenshot = tk.Button(root, text="Capture Full Screen Screenshot", command=capture_screenshot, width=30) |
| 69 | + btn_capture_screenshot.pack(pady=10) |
| 70 | + |
| 71 | + root.mainloop() |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + main() |
0 commit comments