diff --git a/Wrapping/Generators/Python/itk/support/extras.py b/Wrapping/Generators/Python/itk/support/extras.py index fae908161db..d1e95325313 100644 --- a/Wrapping/Generators/Python/itk/support/extras.py +++ b/Wrapping/Generators/Python/itk/support/extras.py @@ -1165,6 +1165,24 @@ 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)): + 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 not supported special characters. \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) diff --git a/Wrapping/Generators/Python/itk/support/template_class.py b/Wrapping/Generators/Python/itk/support/template_class.py index 9601df7976c..cda7d2f8a64 100644 --- a/Wrapping/Generators/Python/itk/support/template_class.py +++ b/Wrapping/Generators/Python/itk/support/template_class.py @@ -157,6 +157,12 @@ 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. + 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 )