{{ message }}
Repository navigation
Expand file tree
/
Copy pathcheck_invisible_text.py
More file actions
287 lines (237 loc) · 10.4 KB
/
Copy pathcheck_invisible_text.py
File metadata and controls
287 lines (237 loc) · 10.4 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "PyMuPDF",
# ]
# ///
import fitz # PyMuPDF
import re
from typing import List, Dict, Tuple
def detect_invisible_text(pdf_path: str) -> Dict[str, List]:
"""
Detect various types of invisible text in a PDF using PyMuPDF.
Returns a dictionary with different categories of invisible text found.
"""
doc = fitz.open(pdf_path)
results = {
"transparent_text": [],
"off_page_text": [],
"zero_size_text": [],
"white_text_on_white": [],
"hidden_text": [],
"summary": {},
}
for page_num in range(len(doc)):
page = doc[page_num]
page_rect = page.rect
# Get all text with detailed formatting information
text_dict = page.get_text("dict")
# Method 1: Check for transparent or nearly transparent text
transparent_text = check_transparent_text(text_dict, page_num)
results["transparent_text"].extend(transparent_text)
# Method 2: Check for text positioned outside visible page area
off_page_text = check_off_page_text(text_dict, page_rect, page_num)
results["off_page_text"].extend(off_page_text)
# Method 3: Check for zero or very small font sizes
zero_size_text = check_zero_size_text(text_dict, page_num)
results["zero_size_text"].extend(zero_size_text)
# Method 4: Check for white text (often invisible on white backgrounds)
white_text = check_white_text(text_dict, page_num)
results["white_text_on_white"].extend(white_text)
# Method 5: Compare extracted text vs visible text
hidden_text = check_hidden_text(page, page_num)
results["hidden_text"].extend(hidden_text)
doc.close()
# Generate summary
total_invisible = sum(
len(category) for category in results.values() if isinstance(category, list)
)
results["summary"] = {
"total_invisible_text_instances": total_invisible,
"pages_with_invisible_text": len(
set(
item["page"]
for category in results.values()
if isinstance(category, list)
for item in category
)
),
"types_found": [
key
for key, value in results.items()
if isinstance(value, list) and len(value) > 0
],
}
return results
def check_transparent_text(text_dict: Dict, page_num: int) -> List[Dict]:
"""Check for transparent or nearly transparent text."""
transparent_texts = []
for block in text_dict.get("blocks", []):
if "lines" in block:
for line in block["lines"]:
for span in line.get("spans", []):
# Check text color for transparency
color = span.get("color", 0)
if color is not None:
# Extract RGB values (PyMuPDF uses integer color values)
r = (color >> 16) & 255
g = (color >> 8) & 255
b = color & 255
# Check for very light colors (near white) or specific transparency indicators
if (r > 250 and g > 250 and b > 250) or color == 0xFFFFFF:
text_content = span.get("text", "").strip()
if text_content:
transparent_texts.append(
{
"page": page_num,
"text": text_content,
"color": f"RGB({r}, {g}, {b})",
"bbox": span.get("bbox"),
"reason": "Very light/white text color",
}
)
return transparent_texts
def check_off_page_text(
text_dict: Dict, page_rect: fitz.Rect, page_num: int
) -> List[Dict]:
"""Check for text positioned outside the visible page area."""
off_page_texts = []
for block in text_dict.get("blocks", []):
if "lines" in block:
for line in block["lines"]:
for span in line.get("spans", []):
bbox = span.get("bbox")
if bbox:
span_rect = fitz.Rect(bbox)
# Check if text is completely outside page boundaries
if not span_rect.intersects(page_rect):
text_content = span.get("text", "").strip()
if text_content:
off_page_texts.append(
{
"page": page_num,
"text": text_content,
"bbox": bbox,
"page_rect": tuple(page_rect),
"reason": "Text positioned outside page boundaries",
}
)
return off_page_texts
def check_zero_size_text(text_dict: Dict, page_num: int) -> List[Dict]:
"""Check for text with zero or very small font sizes."""
zero_size_texts = []
for block in text_dict.get("blocks", []):
if "lines" in block:
for line in block["lines"]:
for span in line.get("spans", []):
font_size = span.get("size", 0)
# Check for zero or very small font sizes
if font_size <= 0.5: # Practically invisible
text_content = span.get("text", "").strip()
if text_content:
zero_size_texts.append(
{
"page": page_num,
"text": text_content,
"font_size": font_size,
"bbox": span.get("bbox"),
"reason": f"Font size too small: {font_size}",
}
)
return zero_size_texts
def check_white_text(text_dict: Dict, page_num: int) -> List[Dict]:
"""Check for white text that might be invisible on white backgrounds."""
white_texts = []
for block in text_dict.get("blocks", []):
if "lines" in block:
for line in block["lines"]:
for span in line.get("spans", []):
color = span.get("color", 0)
if color == 0xFFFFFF or color == 16777215: # Pure white
text_content = span.get("text", "").strip()
if text_content:
white_texts.append(
{
"page": page_num,
"text": text_content,
"color": "White (RGB(255, 255, 255))",
"bbox": span.get("bbox"),
"reason": "White text (invisible on white background)",
}
)
return white_texts
def check_hidden_text(page: fitz.Page, page_num: int) -> List[Dict]:
"""Compare all extractable text vs visually rendered text to find hidden content."""
hidden_texts = []
# Extract all text (including hidden)
all_text = page.get_text()
# Get visible text by rendering the page and using OCR-like extraction
# This is a simplified approach - in practice, you might want more sophisticated methods
visible_text_blocks = page.get_text("blocks")
visible_text = ""
for block in visible_text_blocks:
if isinstance(block, tuple) and len(block) >= 5:
visible_text += block[4] # Text content is at index 4
# Simple comparison - look for significant differences
all_text_clean = re.sub(r"\s+", " ", all_text.strip())
visible_text_clean = re.sub(r"\s+", " ", visible_text.strip())
if len(all_text_clean) > len(visible_text_clean) * 1.1: # 10% threshold
hidden_texts.append(
{
"page": page_num,
"all_text_length": len(all_text_clean),
"visible_text_length": len(visible_text_clean),
"potential_hidden_chars": len(all_text_clean) - len(visible_text_clean),
"reason": "Significant difference between extractable and visible text",
}
)
return hidden_texts
def print_results(results: Dict):
"""Print the results in a readable format."""
print("=" * 60)
print("INVISIBLE TEXT DETECTION RESULTS")
print("=" * 60)
summary = results["summary"]
print(
f"Total invisible text instances found: {summary['total_invisible_text_instances']}"
)
print(f"Pages with invisible text: {summary['pages_with_invisible_text']}")
print(f"Types of invisible text found: {', '.join(summary['types_found'])}")
print()
for category, items in results.items():
if category == "summary" or not items:
continue
print(f"\n{category.upper().replace('_', ' ')}:")
print("-" * 40)
for item in items:
print(f"Page {item['page']}: {item.get('reason', 'Unknown')}")
if "text" in item:
text_preview = (
item["text"][:100] + "..."
if len(item["text"]) > 100
else item["text"]
)
print(f" Text: '{text_preview}'")
if "bbox" in item and item["bbox"]:
print(f" Position: {item['bbox']}")
if "color" in item:
print(f" Color: {item['color']}")
if "font_size" in item:
print(f" Font size: {item['font_size']}")
print()
# Example usage
if __name__ == "__main__":
import sys
if len(sys.argv) != 2:
print("Usage: python script.py <pdf_path>", file=sys.stderr)
sys.exit(1)
pdf_path = sys.argv[1]
try:
results = detect_invisible_text(pdf_path)
print_results(results)
except FileNotFoundError:
print(f"Error: PDF file '{pdf_path}' not found", file=sys.stderr)
sys.exit(1)
except Exception as e:
print(f"Error analyzing PDF: {e}", file=sys.stderr)
sys.exit(1)
You can’t perform that action at this time.
