Repository navigation
Expand file tree
/
Copy pathimaging.py
More file actions
executable file
·50 lines (42 loc) · 1.6 KB
/
Copy pathimaging.py
File metadata and controls
executable file
·50 lines (42 loc) · 1.6 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
import numpy as np
from PIL import Image
import io
from pathlib import Path
# Paths (Windows-friendly)
npz_path = Path(r"./marchive/mfull_archive.npz") # Update full path
output_dir = Path(r"./siim_extracted_images")
output_dir.mkdir(parents=True, exist_ok=True)
# Load with allow_pickle for object arrays
data = np.load(npz_path, allow_pickle=True)
print("Keys:", data.files)
for key in data.files:
arr = data[key]
print(f"{key}: shape {arr.shape if hasattr(arr, 'shape') else 'N/A'}, dtype {arr.dtype}")
if len(arr) > 0:
print(f" Sample type: {type(arr[0])}")
# Extract from 'image' key
if 'image' in data:
images = data['image']
total = len(images)
print(f"Extracting {total} images...")
for i, img_obj in enumerate(images):
try:
if isinstance(img_obj, Image.Image):
img = img_obj
elif isinstance(img_obj, np.ndarray):
img = Image.fromarray(img_obj.astype(np.uint8))
elif isinstance(img_obj, bytes):
img = Image.open(io.BytesIO(img_obj))
else:
# Fallback: treat as pickled image data
img = Image.open(io.BytesIO(img_obj))
# Save PNG
out_path = output_dir / f"siim_{i:05d}.png"
img.save(out_path)
if (i + 1) % 500 == 0:
print(f"Saved {i+1}/{total}")
except Exception as e:
print(f"Error on {i}: {e}")
print(f"Done! Check {output_dir} ({len([p for p in output_dir.glob('*.png')])} PNGs extracted)")
else:
print("No 'image' key found.")