-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_bitmap_header.py
More file actions
94 lines (71 loc) · 3.36 KB
/
fix_bitmap_header.py
File metadata and controls
94 lines (71 loc) · 3.36 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
#!/usr/bin/env python3
"""
Script to fix bitmap headers after Audacity raw export.
Copies the first 255 bytes (0xFF) from the original bitmap to the raw file.
"""
import os
import sys
from pathlib import Path
def fix_bitmap_header(original_bitmap_dir="input-bitmap", raw_dir="output-raw", output_dir="output"):
"""
Fix bitmap headers by copying the first 255 bytes from original to raw files.
Args:
original_bitmap_dir (str): Directory containing original bitmap files
raw_dir (str): Directory containing raw exported files
output_dir (str): Directory to save fixed bitmap files
"""
# Create output directory if it doesn't exist
Path(output_dir).mkdir(exist_ok=True)
# Get paths
original_path = Path(original_bitmap_dir)
raw_path = Path(raw_dir)
output_path = Path(output_dir)
if not original_path.exists():
print(f"Error: Original bitmap directory '{original_bitmap_dir}' does not exist.")
return
if not raw_path.exists():
print(f"Error: Raw files directory '{raw_dir}' does not exist.")
return
# Find all raw files
raw_files = list(raw_path.glob("*.raw"))
if not raw_files:
print(f"No .raw files found in '{raw_dir}' directory.")
return
print(f"Found {len(raw_files)} raw file(s) to fix:")
# Process each raw file
for i, raw_file in enumerate(raw_files, 1):
try:
print(f"[{i}/{len(raw_files)}] Processing {raw_file.name}...")
# Find corresponding original bitmap file
# Remove .raw extension and add .bmp
original_filename = raw_file.stem + '.bmp'
original_file = original_path / original_filename
if not original_file.exists():
print(f" ✗ Original bitmap file '{original_filename}' not found in '{original_bitmap_dir}'")
continue
# Read the first 255 bytes from the original bitmap
with open(original_file, 'rb') as f:
header = f.read(255) # 0xFF bytes
# Read the raw file content
with open(raw_file, 'rb') as f:
raw_content = f.read()
# Combine header with raw content
fixed_content = header + raw_content
# Create output filename
output_filename = raw_file.stem + '.bmp'
output_file = output_path / output_filename
# Write the fixed bitmap
with open(output_file, 'wb') as f:
f.write(fixed_content)
print(f" ✓ Fixed bitmap saved as {output_filename}")
print(f" Original size: {len(raw_content)} bytes")
print(f" Fixed size: {len(fixed_content)} bytes")
except Exception as e:
print(f" ✗ Error processing {raw_file.name}: {e}")
print(f"\nHeader fixing complete! Fixed bitmap files saved to '{output_dir}' directory.")
if __name__ == "__main__":
# Allow command line arguments for directories
original_dir = sys.argv[1] if len(sys.argv) > 1 else "input-bitmap"
raw_dir = sys.argv[2] if len(sys.argv) > 2 else "output-raw"
output_dir = sys.argv[3] if len(sys.argv) > 3 else "output"
fix_bitmap_header(original_dir, raw_dir, output_dir)