-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage_example.py
47 lines (33 loc) · 1.18 KB
/
image_example.py
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
from PIL import Image
import pytesseract
import os
# Define the directory containing the images
dir_path = "/home/linux/TextGenerator-Tesseract/"
# Define Block separator
separator = "=" * 50 # 50 dashes
output_text = ''
with open('ordered.txt', 'r') as f:
filenames = [line.strip() for line in f.readlines()]
# Loop through all files in the directory
for filename in filenames:
if filename.endswith(".jpg") or filename.endswith(".jpeg") or filename.endswith(".png"):
# Open the image file
im = Image.open(os.path.join(dir_path, filename))
# Extract text from the image
text = pytesseract.image_to_string(im, lang='eng').strip() + '\n' + separator + '\n'
# Add the text to the output text
output_text += text
# Write the output text to the output file
with open('output.txt', "w") as f:
f.write(output_text)
#Converting the generated text to word document
import docx
# Open the text file
with open('output.txt', 'r') as file:
text = file.read()
# Create a new Word document
doc = docx.Document()
# Add the text to the document
doc.add_paragraph(text)
# Save the document as a Word file
doc.save('output.docx')