Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Wrapping/Generators/Python/itk/support/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,27 @@ def imwrite(
The writer is instantiated with the image type of the image in
parameter (or, again, with the output image of the filter in parameter).
"""

# Check if output path exists
msg = ""
if not os.path.isdir(os.path.dirname(filename)):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will most likely interfere with Slicer's MRML ID ImageIO. Scratch that - Slicer uses C++ version of ITK for "writing" temporary files.

msg += "\nThe output dir doesn't exist. \n" + f"Filename = {filename}"
raise RuntimeError(
f"Could not create IO object for writing file {filename}" + msg
)
# Check if path contains unsupported special characters
else:
try:
filename.encode("ascii")
except UnicodeEncodeError:
msg += (
"\nThe output path contains unsupported special characters for ascii encoding.\n"
+ f"Filename = {filename}"
)
raise RuntimeError(
f"Could not create IO object for writing file {filename}" + msg
)

import itk

img = itk.output(image_or_filter)
Expand Down
9 changes: 9 additions & 0 deletions Wrapping/Generators/Python/itk/support/template_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ def firstIfList(arg):
msg = ""
if not os.path.isfile(inputFileName):
msg += "\nThe file doesn't exist. \n" + f"Filename = {inputFileName}"
# Check if image path contains not supported special characters.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not supported -> unsupported

else:
try:
inputFileName.encode("ascii")
except UnicodeEncodeError:
msg += (
"\nThe image path contains not supported special characters. \n"
+ f"Filename = {inputFileName}"
)
raise RuntimeError(
f"Could not create IO object for reading file {inputFileName}" + msg
)
Expand Down
Loading