-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTextract.py
More file actions
48 lines (37 loc) · 1.71 KB
/
Textract.py
File metadata and controls
48 lines (37 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
import boto3
from tqdm import tqdm
from PIL import Image
def get_image_files(directory):
"""Get all jpg and png files in the given directory."""
return [f for f in os.listdir(directory) if f.lower().endswith(('.jpg', '.png'))]
def should_process_file(file_path):
"""Check if the file should be processed (i.e., no corresponding txt file exists)."""
txt_path = os.path.splitext(file_path)[0] + '.txt'
return not os.path.exists(txt_path)
def extract_text_from_image(image_path):
"""Extract text from the image using Amazon Textract."""
client = boto3.client('textract')
with open(image_path, 'rb') as image:
response = client.detect_document_text(Document={'Bytes': image.read()})
extracted_text = []
for item in response['Blocks']:
if item['BlockType'] == 'LINE':
extracted_text.append(item['Text'])
return '\n'.join(extracted_text)
def save_text_to_file(text, file_path):
"""Save the extracted text to a file."""
txt_path = os.path.splitext(file_path)[0] + '.txt'
with open(txt_path, 'w', encoding='utf-8') as f:
f.write(text)
def process_images_in_directory(directory):
"""Process all images in the given directory."""
image_files = get_image_files(directory)
for image_file in tqdm(image_files, desc="Processing images"):
image_path = os.path.join(directory, image_file)
if should_process_file(image_path):
extracted_text = extract_text_from_image(image_path)
save_text_to_file(extracted_text, image_path)
# Usage in Jupyter notebook
directory = '.' # Current directory
process_images_in_directory(directory)