Skip to content

Commit e845da6

Browse files
committed
optional bigtiff
1 parent 2f42b07 commit e845da6

1 file changed

Lines changed: 38 additions & 16 deletions

File tree

src/napari_tmidas/_file_conversion.py

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,19 +1105,21 @@ def run(self):
11051105
)
11061106
file_size_GB = estimated_size_bytes / (1024**3)
11071107

1108-
# If file is very large (>4GB), force zarr format regardless of setting
1108+
# Determine format
11091109
use_zarr = self.use_zarr
1110-
if file_size_GB > 4:
1111-
use_zarr = True
1112-
if not self.use_zarr:
1113-
print(
1114-
f"File size ({file_size_GB:.2f}GB) exceeds 4GB limit for TIF, automatically using ZARR format"
1115-
)
1116-
self.file_done.emit(
1117-
filepath,
1118-
True,
1119-
f"File size ({file_size_GB:.2f}GB) exceeds 4GB, using ZARR format",
1120-
)
1110+
# If file is very large (>4GB) and user didn't explicitly choose TIF,
1111+
# auto-switch to ZARR format
1112+
if file_size_GB > 4 and not self.use_zarr:
1113+
# Recommend ZARR format but respect user's choice by still allowing TIF
1114+
print(
1115+
f"File size ({file_size_GB:.2f}GB) exceeds 4GB, ZARR format is recommended but using TIF with BigTIFF format"
1116+
)
1117+
self.file_done.emit(
1118+
filepath,
1119+
True,
1120+
f"File size ({file_size_GB:.2f}GB) exceeds 4GB, using TIF with BigTIFF format",
1121+
)
1122+
11211123
# Set up the output path
11221124
if use_zarr:
11231125
output_path = os.path.join(
@@ -1171,12 +1173,22 @@ def stop(self):
11711173
def _save_tif(
11721174
self, image_data: np.ndarray, output_path: str, metadata: dict = None
11731175
):
1174-
"""Enhanced TIF saving with proper dimension handling"""
1176+
"""Enhanced TIF saving with proper dimension handling and BigTIFF support"""
11751177
import tifffile
11761178

11771179
print(f"Saving TIF file: {output_path}")
11781180
print(f"Image data shape: {image_data.shape}")
11791181

1182+
# Check if this is a large file that needs BigTIFF
1183+
estimated_size_bytes = np.prod(image_data.shape) * image_data.itemsize
1184+
file_size_GB = estimated_size_bytes / (1024**3)
1185+
use_bigtiff = file_size_GB > 4
1186+
1187+
if use_bigtiff:
1188+
print(
1189+
f"File size ({file_size_GB:.2f}GB) exceeds 4GB, using BigTIFF format"
1190+
)
1191+
11801192
if metadata:
11811193
print(f"Metadata keys: {list(metadata.keys())}")
11821194
if "axes" in metadata:
@@ -1198,7 +1210,12 @@ def _save_tif(
11981210
# Basic save if no metadata
11991211
if metadata is None:
12001212
print("No metadata provided, using basic save")
1201-
tifffile.imwrite(output_path, image_data, compression="zstd")
1213+
tifffile.imwrite(
1214+
output_path,
1215+
image_data,
1216+
compression="zstd",
1217+
bigtiff=use_bigtiff,
1218+
)
12021219
return
12031220

12041221
# Get image dimensions and axis order
@@ -1261,7 +1278,10 @@ def _save_tif(
12611278
print(f"Error reordering dimensions: {e}")
12621279
# Fall back to simple save without reordering
12631280
tifffile.imwrite(
1264-
output_path, image_data, compression="zstd"
1281+
output_path,
1282+
image_data,
1283+
compression="zstd",
1284+
bigtiff=use_bigtiff,
12651285
)
12661286
return
12671287

@@ -1288,6 +1308,7 @@ def _save_tif(
12881308
image_data,
12891309
resolution=resolution,
12901310
compression="zstd",
1311+
bigtiff=use_bigtiff,
12911312
)
12921313
else:
12931314
# Hyperstack case
@@ -1307,13 +1328,14 @@ def _save_tif(
13071328
resolution=resolution,
13081329
metadata=imagej_metadata,
13091330
compression="zstd",
1331+
bigtiff=use_bigtiff,
13101332
)
13111333

13121334
print(f"Successfully saved TIF file: {output_path}")
13131335
except (ValueError, FileNotFoundError) as e:
13141336
print(f"Error saving TIF file: {e}")
13151337
# Try simple save as fallback
1316-
tifffile.imwrite(output_path, image_data)
1338+
tifffile.imwrite(output_path, image_data, bigtiff=use_bigtiff)
13171339

13181340
def _save_zarr(
13191341
self, image_data: np.ndarray, output_path: str, metadata: dict = None

0 commit comments

Comments
 (0)