Skip to content

Commit 35f0771

Browse files
authored
Screenshot to Text (DhanushNehru#320)
1 parent 1016399 commit 35f0771

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

Screenshot to Text/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# OCR Text Extractor
2+
3+
## Overview
4+
**OCR Text Extractor** is a Python application that allows users to extract text from images using Optical Character Recognition (OCR) via the Tesseract engine. The application provides a simple and user-friendly graphical interface for uploading images, capturing full-screen screenshots, or pasting images from the clipboard to extract text.
5+
6+
## Features
7+
- **Select Image File**: Choose an image from your computer to extract text from. Supported formats include `.png`, `.jpg`, `.jpeg`, and `.bmp`.
8+
- **Paste Image from Clipboard**: Take a partial screenshot (e.g., using Windows + Shift + S) and extract text from the image saved in the clipboard.
9+
- **Capture Full Screen Screenshot**: Capture a screenshot of your entire screen and extract text from it.
10+
11+
## Requirements
12+
To run this application, you need to have the following installed:
13+
14+
- Python 3.x
15+
- The following Python packages:
16+
- `pytesseract` (Download Tesseract OCR from https://github.com/UB-Mannheim/tesseract/wiki and Add to Environment Path)
17+
- `Pillow` (Python Imaging Library, a fork of PIL)
18+
- `Tkinter` (comes pre-installed with Python on most systems)
19+
20+
### Installation of Required Packages
21+
You can install the required packages using pip:
22+
23+
```bash
24+
pip install pytesseract Pillow
25+
```
26+
27+
### How to Run
28+
```bash
29+
python main.py
30+
```

Screenshot to Text/main.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

Comments
 (0)