forked from Avika2211/pdf-image-classifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfigure_extractor.py
More file actions
222 lines (169 loc) · 7.99 KB
/
figure_extractor.py
File metadata and controls
222 lines (169 loc) · 7.99 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import fitz # PyMuPDF
from PIL import Image
import io
import logging
class PDFFigureExtractor:
"""Extract figures and images from PDF documents."""
def __init__(self):
self.logger = logging.getLogger(__name__)
def extract_figures(self, pdf_path):
"""
Extract all figures from a PDF file.
Args:
pdf_path (str): Path to the PDF file
Returns:
list: List of dictionaries containing figure data
"""
figures = []
try:
# Open PDF document
doc = fitz.open(pdf_path)
for page_num in range(len(doc)):
page = doc.load_page(page_num)
# Extract images from page
image_list = page.get_images(full=True)
for img_index, img in enumerate(image_list):
try:
# Get image data
xref = img[0]
pix = fitz.Pixmap(doc, xref)
# Skip if image is too small (likely decorative)
if pix.width < 50 or pix.height < 50:
pix = None
continue
# Convert to PIL Image
if pix.n - pix.alpha < 4: # GRAY or RGB
img_data = pix.tobytes("png")
img_pil = Image.open(io.BytesIO(img_data))
else: # CMYK
pix_rgb = fitz.Pixmap(fitz.csRGB, pix)
img_data = pix_rgb.tobytes("png")
img_pil = Image.open(io.BytesIO(img_data))
pix_rgb = None
# Get image position and size
img_rect = page.get_image_rects(img)[0] if page.get_image_rects(img) else None
# Store figure data
figure_data = {
'image': img_pil,
'page': page_num + 1,
'index': img_index,
'bbox': img_rect,
'width': pix.width,
'height': pix.height,
'size': len(img_data)
}
figures.append(figure_data)
# Clean up
pix = None
except Exception as e:
self.logger.warning(f"Error extracting image {img_index} from page {page_num + 1}: {str(e)}")
continue
# Also extract vector graphics as images
vector_figures = self._extract_vector_graphics(page, page_num + 1)
figures.extend(vector_figures)
doc.close()
except Exception as e:
self.logger.error(f"Error processing PDF: {str(e)}")
raise
return figures
def _extract_vector_graphics(self, page, page_num):
"""
Extract vector graphics from a page by rendering them as images.
Args:
page: PyMuPDF page object
page_num: Page number
Returns:
list: List of vector graphics as images
"""
vector_figures = []
try:
# Get page drawings (vector graphics)
drawings = page.get_drawings()
if not drawings:
return vector_figures
# Group drawings by proximity to identify figures
figure_groups = self._group_drawings_by_proximity(drawings)
for group_idx, drawing_group in enumerate(figure_groups):
try:
# Calculate bounding box for the group
min_x = min(d['rect'][0] for d in drawing_group)
min_y = min(d['rect'][1] for d in drawing_group)
max_x = max(d['rect'][2] for d in drawing_group)
max_y = max(d['rect'][3] for d in drawing_group)
# Skip if too small
if (max_x - min_x) < 50 or (max_y - min_y) < 50:
continue
# Create clipping rectangle
clip_rect = fitz.Rect(min_x, min_y, max_x, max_y)
# Render the clipped area as image
mat = fitz.Matrix(2, 2) # 2x zoom for better quality
pix = page.get_pixmap(matrix=mat, clip=clip_rect)
# Convert to PIL Image
img_data = pix.tobytes("png")
img_pil = Image.open(io.BytesIO(img_data))
# Store vector figure data
figure_data = {
'image': img_pil,
'page': page_num,
'index': f"vector_{group_idx}",
'bbox': clip_rect,
'width': pix.width,
'height': pix.height,
'size': len(img_data),
'type': 'vector'
}
vector_figures.append(figure_data)
# Clean up
pix = None
except Exception as e:
self.logger.warning(f"Error extracting vector graphic {group_idx} from page {page_num}: {str(e)}")
continue
except Exception as e:
self.logger.warning(f"Error extracting vector graphics from page {page_num}: {str(e)}")
return vector_figures
def _group_drawings_by_proximity(self, drawings, threshold=50):
"""
Group drawings by proximity to identify coherent figures.
Args:
drawings: List of drawing objects
threshold: Distance threshold for grouping
Returns:
list: List of drawing groups
"""
if not drawings:
return []
groups = []
used_indices = set()
for i, drawing in enumerate(drawings):
if i in used_indices:
continue
current_group = [drawing]
used_indices.add(i)
# Find nearby drawings
for j, other_drawing in enumerate(drawings):
if j in used_indices:
continue
if self._are_drawings_close(drawing, other_drawing, threshold):
current_group.append(other_drawing)
used_indices.add(j)
# Only keep groups with substantial content
if len(current_group) >= 2:
groups.append(current_group)
return groups
def _are_drawings_close(self, drawing1, drawing2, threshold):
"""
Check if two drawings are close enough to be part of the same figure.
Args:
drawing1, drawing2: Drawing objects
threshold: Distance threshold
Returns:
bool: True if drawings are close
"""
rect1 = drawing1['rect']
rect2 = drawing2['rect']
# Calculate centers
center1 = ((rect1[0] + rect1[2]) / 2, (rect1[1] + rect1[3]) / 2)
center2 = ((rect2[0] + rect2[2]) / 2, (rect2[1] + rect2[3]) / 2)
# Calculate distance
distance = ((center1[0] - center2[0]) ** 2 + (center1[1] - center2[1]) ** 2) ** 0.5
return distance < threshold