33import skimage .io as sio
44from pathlib import Path
55
6- def process_nd2_to_quadrants (nd2_file ):
7- """Convert ND2 to TIF and split into 4 quadrants (512x512 each)"""
6+
7+ def process_nd2_to_sixteenths (nd2_file ):
8+ """Convert ND2 to TIF and split into 16 tiles (256x256 each)"""
89 # Read ND2 file
910 with ND2Reader (nd2_file ) as images :
1011 img = np .array (images )
1112
1213 # Pad to 1024x1024 if needed
1314 oshape = img .shape
14- padded = np .zeros ((oshape [0 ], 1024 , 1024 ), dtype = img .dtype )
15+ padded = np .zeros ((oshape [0 ], 2044 , 2048 ), dtype = img .dtype )
1516 padded [:, :oshape [1 ], :oshape [2 ]] = img
1617
1718 # Get original file's directory and name
@@ -23,22 +24,22 @@ def process_nd2_to_quadrants(nd2_file):
2324 full_tif = output_dir / f"{ output_name } .tif"
2425 sio .imsave (str (full_tif ), padded )
2526
26- # Split into quadrants and save in original directory
27- quadrant_files = []
28- for row in range (2 ):
29- for col in range (2 ):
30- quadrant = padded [:, row * 512 :(row + 1 )* 512 , col * 512 :(col + 1 )* 512 ]
27+ # Split into 16ths (4x4 grid of 256x256 tiles) and save in original directory
28+ tile_files = []
29+ for row in range (4 ):
30+ for col in range (4 ):
31+ tile = padded [:, row * 256 :(row + 1 )* 256 , col * 256 :(col + 1 )* 256 ]
3132 filename = output_dir / f"{ output_name } _{ row } _{ col } .tif"
32- sio .imsave (str (filename ), quadrant )
33- quadrant_files .append (str (filename ))
33+ sio .imsave (str (filename ), tile )
34+ tile_files .append (str (filename ))
3435
35- return quadrant_files
36+ return tile_files
3637
3738# Usage
3839if __name__ == "__main__" :
3940 import sys
4041 if len (sys .argv ) > 1 :
41- files = process_nd2_to_quadrants (sys .argv [1 ])
42- print (f"Created: { ', ' .join (files )} " )
42+ files = process_nd2_to_sixteenths (sys .argv [1 ])
43+ print (f"Created { len ( files ) } tiles : { ', ' .join (files )} " )
4344 else :
4445 print ("Usage: python script.py <file.nd2>" )
0 commit comments