-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
97 lines (78 loc) · 3.23 KB
/
Copy pathmain.py
File metadata and controls
97 lines (78 loc) · 3.23 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
import nltk
from PIL import Image, ImageDraw, ImageFont
from pdf2image import convert_from_path
from fpdf import FPDF
from unstructured.partition.pdf import partition_pdf
from collections import Counter
from unstructured.staging.base import convert_to_dict
from collections import defaultdict
import shutil
# Dictionary for colors for each type
color_dict = {
"Title": "red",
"FigureCaption": "blue",
"NarrativeText": "green",
"ListItem": "yellow",
"Table": "orange",
"Address": "purple",
"PageBreak": "brown"
}
# Paths
input_pdf_path = "C:/Users/jatin/OneDrive/Desktop/ai/pdf_2.pdf"
# Annotate PDF
annotate_pdf(input_pdf_path)
def parse_pdf(pdf_path):
elements = partition_pdf(pdf_path,strategy="hi_res")
list_elements(elements)
return convert_to_dict(elements)
def list_elements(elements)
print("List of elements identified:\n")
display(Counter(type(element) for element in elements))
def copy_pdf(pdf_path):
directory, file_name = os.path.split(pdf_path)
base_name, ext = os.path.splitext(file_name);
new_file_name = f"{base_name}_annotate{ext}"
new_file_path = os.path.join(directory, new_file_name)
shutil.copy(file_path, new_file_path)
return new_file_path
def annotate_pdf(pdf_path):
data = parse_pdf(pdf_path);
# Convert the pdf pages into images
images = convert_from_path(pdf_path)
# Load the font and set the size
font_size = 30 # Change this value to increase or decrease the text size
font = ImageFont.truetype("arial.ttf", font_size)
# Create a dictionary mapping page numbers to list of data
page_data_dict = defaultdict(list)
for item in data:
page_data_dict[item['metadata']['page_number']].append(item)
# Iterate through pages in ascending order
for page_number in sorted(page_data_dict.keys()):
# Fetch the corresponding image based on page number
img = images[page_number - 1]
draw = ImageDraw.Draw(img)
# Iterate through each data entry for this page
for page_data in page_data_dict[page_number]:
# Fetch the color for the type
color = color_dict.get(page_data['type'], "blue") # default color is blue
# Draw the bounding box
draw.rectangle(
[(page_data['coordinates'][0][0], page_data['coordinates'][0][1]), (page_data['coordinates'][2][0], page_data['coordinates'][2][1])],
outline=color,
width=2
)
# Write the type next to the bounding box with the new font size
draw.text((page_data['coordinates'][0][0], page_data['coordinates'][0][1] - 10), page_data['type'], fill=color, font=font)
# Save the image
img.save(f"temp_img_{page_number}.png")
# Now we convert these images back into a PDF
pdf = FPDF()
# Only include images for pages for which we have data
for page_number in sorted(page_data_dict.keys()):
img_path = f"temp_img_{page_number}.png"
pdf.add_page()
pdf.image(img_path, x=0, y=0, w=210, h=297) # width and height are in millimeters
os.remove(img_path) # Remove the temporary image
output_pdf_path = copy_pdf(pdf_path)
pdf.output(output_pdf_path)