Skip to content

Commit b809c77

Browse files
committed
fix: print install hint when no suitable ImageWriter found
When resolve_writer() fails to find a backend for a given file extension, the error was generic. Added a format-to-package lookup that appends an install hint (e.g. pip install nibabel) for common formats. Fixes #7980 Signed-off-by: yashnaiduu <yashnaiduu@users.noreply.github.com>
1 parent 1e3d29b commit b809c77

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

monai/data/image_writer.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,23 @@ def resolve_writer(ext_name, error_if_not_found=True) -> Sequence:
116116
except Exception: # other writer init errors indicating it exists
117117
avail_writers.append(_writer)
118118
if not avail_writers and error_if_not_found:
119-
raise OptionalImportError(f"No ImageWriter backend found for {fmt}.")
119+
# map common extensions to their required package
120+
install_hints: dict = {
121+
"nii": "nibabel",
122+
"nii.gz": "nibabel",
123+
"mha": "itk",
124+
"mhd": "itk",
125+
"nrrd": "itk",
126+
"png": "pillow",
127+
"jpg": "pillow",
128+
"jpeg": "pillow",
129+
"tif": "pillow",
130+
"tiff": "pillow",
131+
"bmp": "pillow",
132+
}
133+
hint = install_hints.get(fmt)
134+
extra = f" Try installing the required package: pip install {hint}" if hint else ""
135+
raise OptionalImportError(f"No ImageWriter backend found for '{fmt}'.{extra}")
120136
writer_tuple = ensure_tuple(avail_writers)
121137
SUPPORTED_WRITERS[fmt] = writer_tuple
122138
return writer_tuple

0 commit comments

Comments
 (0)