-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBeast Compression.py
More file actions
481 lines (397 loc) · 17.7 KB
/
Copy pathBeast Compression.py
File metadata and controls
481 lines (397 loc) · 17.7 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#!/usr/bin/env python3
"""
Beast PDF Compressor - Maximum Text-Only Compression
Sacrifices all image quality for maximum file size reduction while preserving text readability.
Designed for archiving documents where only text content matters.
"""
import os
import sys
import subprocess
import shutil
from pathlib import Path
import argparse
import time
class BeastPDFCompressor:
def __init__(self, ghostscript_path=None):
"""Initialize the Beast PDF Compressor"""
self.gs_path = self._find_ghostscript(ghostscript_path)
if not self.gs_path:
raise Exception("Ghostscript not found! Please install Ghostscript.")
def _find_ghostscript(self, custom_path=None):
"""Find Ghostscript executable in common locations"""
possible_paths = []
if custom_path:
possible_paths.append(custom_path)
# Common installation paths
possible_paths.extend([
r"S:\gs10.05.1\bin\gswin64c.exe",
r"S:\gs10.05.1\bin\gswin32c.exe",
r"C:\Program Files\gs\gs10.05.1\bin\gswin64c.exe",
r"C:\Program Files\gs\gs9.56.1\bin\gswin64c.exe",
r"C:\Program Files (x86)\gs\gs10.05.1\bin\gswin32c.exe",
r"C:\Program Files (x86)\gs\gs9.56.1\bin\gswin32c.exe"
])
# Try system PATH
possible_paths.extend(["gswin64c.exe", "gswin32c.exe", "gs"])
for path in possible_paths:
if shutil.which(path) or os.path.exists(path):
return path
return None
def get_file_size(self, filepath):
"""Get file size in bytes"""
return os.path.getsize(filepath)
def format_size(self, size_bytes):
"""Format file size in human readable format"""
if size_bytes < 1024:
return f"{size_bytes} B"
elif size_bytes < 1024 ** 2:
return f"{size_bytes / 1024:.1f} KB"
elif size_bytes < 1024 ** 3:
return f"{size_bytes / (1024 ** 2):.1f} MB"
else:
return f"{size_bytes / (1024 ** 3):.1f} GB"
def analyze_pdf(self, input_path):
"""Analyze PDF file for optimization strategy"""
try:
file_size = self.get_file_size(input_path)
# Quick analysis based on file size
if file_size > 50 * 1024 * 1024: # >50MB
analysis_type = "Large Document (likely scanned)"
elif file_size > 10 * 1024 * 1024: # >10MB
analysis_type = "Medium Document (mixed content)"
else:
analysis_type = "Small Document (likely text-heavy)"
return {
'file_size': file_size,
'type': analysis_type,
'needs_aggressive_compression': file_size > 5 * 1024 * 1024
}
except Exception as e:
return {
'file_size': 0,
'type': 'Unknown',
'needs_aggressive_compression': True,
'error': str(e)
}
def beast_compress(self, input_path, output_path):
"""
Apply Beast compression - maximum compression with text preservation
"""
print("BEAST COMPRESSION INITIATED")
print("WARNING: All image quality will be sacrificed for maximum compression")
print("Text readability will be preserved")
# Beast mode compression settings
beast_cmd = [
self.gs_path,
'-sDEVICE=pdfwrite',
'-dCompatibilityLevel=1.4',
'-dNOPAUSE',
'-dQUIET',
'-dBATCH',
'-dSAFER',
# Optimization settings
'-dOptimize=true',
'-dFastWebView=true',
'-dPrinted=false',
'-dUseFlateCompression=true',
# Font optimization
'-dEmbedAllFonts=false',
'-dSubsetFonts=true',
'-dCompressFonts=true',
# Image destruction for maximum compression
'-dPDFSETTINGS=/screen',
'-dDownsampleColorImages=true',
'-dDownsampleGrayImages=true',
'-dDownsampleMonoImages=true',
'-dColorImageDownsampleType=/Subsample',
'-dGrayImageDownsampleType=/Subsample',
'-dMonoImageDownsampleType=/Subsample',
# Ultra-low resolution settings
'-dColorImageResolution=50',
'-dGrayImageResolution=50',
'-dMonoImageResolution=150',
# Aggressive threshold settings
'-dColorImageDownsampleThreshold=1.0',
'-dGrayImageDownsampleThreshold=1.0',
'-dMonoImageDownsampleThreshold=1.0',
# Image encoding settings
'-dEncodeColorImages=true',
'-dEncodeGrayImages=true',
'-dEncodeMonoImages=true',
'-dColorImageFilter=/DCTEncode',
'-dGrayImageFilter=/DCTEncode',
'-dMonoImageFilter=/CCITTFaxEncode',
# Minimum quality settings
'-dJPEGQ=10',
'-dAutoFilterColorImages=false',
'-dAutoFilterGrayImages=false',
'-dAntiAliasColorImages=false',
'-dAntiAliasGrayImages=false',
'-dAntiAliasMonoImages=false',
# Remove unnecessary elements
'-dDetectDuplicateImages=true',
'-dPreserveAnnots=false',
'-dPreserveMarkedContent=false',
'-dPreserveOPIComments=false',
'-dPreserveHalftoneInfo=false',
'-dPreserveOverprintSettings=false',
'-dPreserveEPSInfo=false',
# Color optimization
'-dUseCIEColor=false',
'-dColorConversionStrategy=/LeaveColorUnchanged',
'-dConvertCMYKImagesToRGB=true',
'-dConvertImagesToIndexed=true',
'-dUCRandBGInfo=/Remove',
# Bypass image preservation
'-dPassThroughJPEGImages=false',
'-dPassThroughJPEGQ=false',
# Output file
f'-sOutputFile={output_path}',
input_path
]
try:
print("Executing Beast compression...")
start_time = time.time()
result = subprocess.run(beast_cmd, capture_output=True, text=True, timeout=600)
end_time = time.time()
compression_time = end_time - start_time
if result.returncode != 0:
print(f"Primary Beast compression failed (exit code: {result.returncode})")
print("Attempting fallback compression...")
return self._fallback_compression(input_path, output_path)
if not os.path.exists(output_path):
print("Output file was not created, attempting fallback...")
return self._fallback_compression(input_path, output_path)
print(f"Beast compression completed in {compression_time:.2f} seconds")
return True
except subprocess.TimeoutExpired:
print("Beast compression timed out (10 minutes), attempting fallback...")
return self._fallback_compression(input_path, output_path)
except Exception as e:
print(f"Beast compression error: {e}")
return self._fallback_compression(input_path, output_path)
def _fallback_compression(self, input_path, output_path):
"""Fallback compression when Beast mode fails"""
print("FALLBACK: Attempting text-extraction focused compression...")
fallback_cmd = [
self.gs_path,
'-sDEVICE=pdfwrite',
'-dCompatibilityLevel=1.4',
'-dNOPAUSE',
'-dQUIET',
'-dBATCH',
'-dSAFER',
'-dPDFSETTINGS=/screen',
'-dOptimize=true',
'-dEmbedAllFonts=false',
'-dSubsetFonts=true',
'-dCompressFonts=true',
'-dEncodeColorImages=false',
'-dEncodeGrayImages=false',
'-dEncodeMonoImages=true',
'-dPreserveAnnots=false',
'-dPreserveMarkedContent=false',
'-dUseFlateCompression=true',
f'-sOutputFile={output_path}',
input_path
]
try:
result = subprocess.run(fallback_cmd, capture_output=True, text=True, timeout=300)
if result.returncode == 0 and os.path.exists(output_path):
print("Fallback compression successful")
return True
else:
print("Fallback compression also failed")
return False
except Exception as e:
print(f"Fallback compression error: {e}")
return False
def _analyze_compression_result(self, original_size, compressed_size):
"""Analyze and explain compression results"""
print("\nCOMPRESSION ANALYSIS:")
print("-" * 50)
if compressed_size < original_size:
reduction = ((original_size - compressed_size) / original_size) * 100
saved_space = original_size - compressed_size
print(f"Space saved: {self.format_size(saved_space)}")
print(f"Compression ratio: {reduction:.1f}%")
if reduction > 95:
print("Result: EXCEPTIONAL - Beast mode achieved maximum compression")
elif reduction > 90:
print("Result: EXCELLENT - Very high compression achieved")
elif reduction > 70:
print("Result: GOOD - Solid compression achieved")
elif reduction > 50:
print("Result: MODERATE - Some compression achieved")
else:
print("Result: LIMITED - Minimal compression achieved")
elif compressed_size == original_size:
print("Result: NO CHANGE - File may already be optimized")
else:
increase = ((compressed_size - original_size) / original_size) * 100
print(f"Result: SIZE INCREASED by {increase:.1f}%")
print("This can happen with already optimized text-only PDFs")
def _explain_beast_compression(self):
"""Explain what Beast compression does"""
print("\nBEAST COMPRESSION DETAILS:")
print("-" * 50)
print("SACRIFICED FOR MAXIMUM COMPRESSION:")
print("• All image quality reduced to minimum (10% JPEG quality)")
print("• Image resolution reduced to 50 DPI for color/grayscale")
print("• Font embedding minimized to essential characters only")
print("• All metadata, annotations, and comments removed")
print("• Color profiles and advanced graphics removed")
print("• Duplicate images and objects eliminated")
print("\nPRESERVED FOR READABILITY:")
print("• All text content and formatting")
print("• Document structure and page layout")
print("• Essential navigation elements")
print("• Basic font rendering")
def process_single_file(self, input_file, output_dir=None):
"""Process a single PDF file with Beast compression"""
input_path = Path(input_file)
if not input_path.exists():
print(f"ERROR: Input file {input_file} does not exist")
return False
if not input_path.suffix.lower() == '.pdf':
print(f"ERROR: {input_file} is not a PDF file")
return False
# Determine output path
if output_dir:
output_dir = Path(output_dir)
output_dir.mkdir(exist_ok=True)
output_path = output_dir / f"{input_path.stem}_text_only.pdf"
else:
output_path = input_path.parent / f"{input_path.stem}_text_only.pdf"
print(f"Processing: {input_path.name}")
print(f"Output: {output_path}")
print("=" * 60)
# Analyze the PDF
analysis = self.analyze_pdf(input_path)
original_size = analysis['file_size']
print(f"File type: {analysis['type']}")
print(f"Original size: {self.format_size(original_size)}")
# Perform Beast compression
success = self.beast_compress(str(input_path), str(output_path))
if success:
compressed_size = self.get_file_size(output_path)
print(f"Compressed size: {self.format_size(compressed_size)}")
# Analyze results
self._analyze_compression_result(original_size, compressed_size)
return True
else:
print("Beast compression failed completely")
return False
def process_directory(self, input_dir, output_dir=None):
"""Process all PDF files in a directory"""
input_path = Path(input_dir)
if not input_path.exists():
print(f"ERROR: Input directory {input_dir} does not exist")
return
# Find all PDF files
pdf_files = list(input_path.glob("*.pdf"))
if not pdf_files:
print(f"No PDF files found in {input_dir}")
return
print(f"BEAST COMPRESSION BATCH PROCESSING")
print(f"Found {len(pdf_files)} PDF files to process")
print("=" * 60)
success_count = 0
total_original_size = 0
total_compressed_size = 0
start_time = time.time()
for i, pdf_file in enumerate(pdf_files, 1):
print(f"\nProcessing file {i}/{len(pdf_files)}")
print("-" * 40)
original_size = self.get_file_size(pdf_file)
total_original_size += original_size
if self.process_single_file(pdf_file, output_dir):
success_count += 1
# Calculate compressed size for totals
if output_dir:
compressed_file = Path(output_dir) / f"{pdf_file.stem}_text_only.pdf"
else:
compressed_file = pdf_file.parent / f"{pdf_file.stem}_text_only.pdf"
if compressed_file.exists():
total_compressed_size += self.get_file_size(compressed_file)
# Final summary
end_time = time.time()
total_time = end_time - start_time
print(f"\nBEAST COMPRESSION BATCH COMPLETE")
print("=" * 60)
print(f"Total processing time: {total_time:.2f} seconds")
print(f"Files processed: {len(pdf_files)}")
print(f"Successful compressions: {success_count}")
print(f"Failed compressions: {len(pdf_files) - success_count}")
print(f"Total original size: {self.format_size(total_original_size)}")
print(f"Total compressed size: {self.format_size(total_compressed_size)}")
if total_original_size > 0 and total_compressed_size > 0:
if total_compressed_size < total_original_size:
total_reduction = ((total_original_size - total_compressed_size) / total_original_size) * 100
total_saved = total_original_size - total_compressed_size
print(f"Overall compression: {total_reduction:.1f}%")
print(f"Total space saved: {self.format_size(total_saved)}")
if total_reduction > 90:
print("RESULT: EXCEPTIONAL BEAST COMPRESSION ACHIEVED")
elif total_reduction > 70:
print("RESULT: EXCELLENT BEAST COMPRESSION ACHIEVED")
elif total_reduction > 50:
print("RESULT: GOOD BEAST COMPRESSION ACHIEVED")
else:
print("RESULT: MODERATE BEAST COMPRESSION ACHIEVED")
# Show compression explanation
self._explain_beast_compression()
def main():
"""Main function to handle command line arguments and execute compression"""
parser = argparse.ArgumentParser(
description='Beast PDF Compressor - Maximum text-only compression',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
BEAST COMPRESSION:
Sacrifices all image quality for maximum file size reduction while preserving text readability.
Perfect for archiving documents where only text content matters.
EXAMPLES:
python beast_compressor.py document.pdf
python beast_compressor.py ./documents -o ./compressed
python beast_compressor.py "C:/My Documents/file.pdf"
"""
)
parser.add_argument('input', help='Input PDF file or directory path')
parser.add_argument('-o', '--output', help='Output directory (optional)')
parser.add_argument('-g', '--ghostscript', help='Path to Ghostscript executable (optional)')
args = parser.parse_args()
try:
compressor = BeastPDFCompressor(args.ghostscript)
input_path = Path(args.input)
if input_path.is_file():
compressor.process_single_file(args.input, args.output)
elif input_path.is_dir():
compressor.process_directory(args.input, args.output)
else:
print(f"ERROR: {args.input} is not a valid file or directory")
sys.exit(1)
except Exception as e:
print(f"ERROR: {e}")
sys.exit(1)
if __name__ == "__main__":
if len(sys.argv) == 1:
print("BEAST PDF COMPRESSOR - TEXT-ONLY FOCUS")
print("=" * 50)
print("WARNING: This will sacrifice all image quality for maximum compression!")
print("Perfect for archiving documents where only text matters.")
print("\nUSAGE:")
print("python beast_compressor.py <input_file_or_directory> [options]")
print("\nEXAMPLES:")
print("python beast_compressor.py document.pdf")
print("python beast_compressor.py ./documents -o ./compressed")
print('python beast_compressor.py "C:/My Documents/file.pdf"')
print("\nFEATURES:")
print("• Maximum compression with text preservation")
print("• Automatic fallback for problematic PDFs")
print("• Detailed compression statistics")
print("• Batch processing support")
print("• Intelligent file analysis")
print("\nDrag and drop files onto this script to compress them!")
input("\nPress Enter to exit...")
else:
main()