File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed
OCR Image-to-Text Conversion Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ import pytesseract
2+ from PIL import Image
3+ import os
4+
5+ def extract_text_from_image (image_path ):
6+ """Extracts text from an image file using Tesseract OCR."""
7+ try :
8+ # Open the image file
9+ with Image .open (image_path ) as img :
10+ # Use pytesseract to do OCR on the image
11+ extracted_text = pytesseract .image_to_string (img )
12+ return extracted_text
13+ except Exception as e :
14+ print (f"Error processing the image: { e } " )
15+ return None
16+
17+ def main ():
18+
19+ image_path = input ("Enter the path to the image file: " )
20+
21+ if not os .path .isfile (image_path ):
22+ print ("File does not exist. Please check the path and try again." )
23+ return
24+ text = extract_text_from_image (image_path )
25+
26+ if text :
27+ print ("Extracted Text:" )
28+ print (text )
29+
30+ if __name__ == "__main__" :
31+ main ()
Original file line number Diff line number Diff line change 1+ pillow == 11.0.0
2+ pytesseract == 0.3.13
You can’t perform that action at this time.
0 commit comments