forked from SomilGupta98/ISRO_BAH
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathh5conversion.py
More file actions
33 lines (26 loc) · 1.05 KB
/
h5conversion.py
File metadata and controls
33 lines (26 loc) · 1.05 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
#script to extract MIR image from HDF5 file and save as PNG
import h5py
import numpy as np
from PIL import Image
import os
count = 2
def extract_mir_image(h5_file, output_path=f"{count}.png"):
global count
with h5py.File(h5_file, 'r') as f:
img_data = f['IMG_MIR'][0] # Shape: (1616, 1737)
# Normalize to 0-255 for 8-bit grayscale image
img_min = img_data.min()
img_max = img_data.max()
print(f"Original value range: {img_min} to {img_max}")
# Handle special fill values (1023)
fill_value = f['IMG_MIR'].attrs.get('_FillValue', [None])[0]
if fill_value is not None:
img_data = np.where(img_data == fill_value, 0, img_data)
# Normalize and clip
norm_img = ((img_data - img_min) / (img_max - img_min) * 255).astype(np.uint8)
# Save image
Image.fromarray(norm_img).save(output_path)
print(f"Saved image as: {output_path}")
count += 1
# Example usage
extract_mir_image("3RIMG_01JUL2025_0115_L1C_ASIA_MER_V01R00_B5.h5")