Skip to content

Commit 2d423b8

Browse files
committed
feat: add installation hints for missing image writers #7980
1 parent a234edf commit 2d423b8

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

monai/data/image_writer.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,26 @@ 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+
RECOMMENDED_PACKAGES = {
120+
"png": "Pillow",
121+
"jpg": "Pillow",
122+
"jpeg": "Pillow",
123+
"nii": "nibabel or SimpleITK",
124+
"nii.gz": "nibabel or SimpleITK",
125+
"nrrd": "pynrrd",
126+
"tif": "Pillow or tifffile",
127+
"tiff": "Pillow or tifffile",
128+
}
129+
130+
fmt_clean = fmt.replace(".", "").lower()
131+
package_hint = RECOMMENDED_PACKAGES.get(fmt_clean, "")
132+
133+
msg = f"No ImageWriter backend found for {fmt}."
134+
if package_hint:
135+
msg += f" Please install '{package_hint}' (e.g., pip install {package_hint})."
136+
137+
raise OptionalImportError(msg)
138+
120139
writer_tuple = ensure_tuple(avail_writers)
121140
SUPPORTED_WRITERS[fmt] = writer_tuple
122141
return writer_tuple

0 commit comments

Comments
 (0)