Repository navigation
Expand file tree
/
Copy pathscript.py
More file actions
executable file
·57 lines (42 loc) · 1.58 KB
/
Copy pathscript.py
File metadata and controls
executable file
·57 lines (42 loc) · 1.58 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
import struct
import numpy as np
import matplotlib.pyplot as plt
import sys
def read_bim(filename):
with open(filename, "rb") as f:
# --- Header ---
header = f.read(4)
if len(header) != 4:
raise ValueError("Invalid or corrupted header")
width, height = struct.unpack("<HH", header)
print(f"Image size: {width} x {height}")
bytes_per_row = (width + 7) // 8
total_bytes = bytes_per_row * height
data = f.read(total_bytes)
if len(data) != total_bytes:
raise ValueError("File truncated or corrupted")
# --- Unpack bits row by row (MSB first, padded per row) ---
bitmap = np.zeros((height, width), dtype=np.uint8)
for y in range(height):
row_start = y * bytes_per_row
row_bytes = data[row_start:row_start + bytes_per_row]
# unpack MSB→LSB for each byte
bits = np.unpackbits(np.frombuffer(row_bytes, dtype=np.uint8))
bitmap[y, :] = bits[:width] # discard padding bits
return bitmap
def show_bitmap(bitmap, invert=False, save=True, show=False):
if invert:
bitmap = 1 - bitmap # optional inversion
plt.imshow(bitmap, cmap="gray", interpolation="nearest")
plt.axis("off")
if save:
plt.savefig("output.png", bbox_inches="tight", pad_inches=0)
if show:
plt.show()
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python script.py <file.bim>")
sys.exit(1)
filename = sys.argv[1]
bmp = read_bim(filename)
show_bitmap(bmp, invert=True)