std::string to char const * in python itk for itk.ImageIOBase`


I am quite new to ITK and trying to make a very simple application using ITK / python!

I am doing something like this.

filename = "./output_filename.hdr"
image_io = itk.ImageIOBase

I mean, I know the function itself explains a whole lot but it gives me an error saying

TypeError: Wrong number or type of arguments for overloaded function 'itkImageIOBase_SetFileName'.
  Possible C/C++ prototypes are:
    itkImageIOBase::SetFileName(char const *)
    itkImageIOBase::SetFileName(std::string const &)

I understand the filename is str in python and that converts to std::string when it reroutes to cxx code.
Is there any way I can declare the filename using that function? Do I really have to use c++ instead? (Simple ITK was not that satisfying for me …)

I don’t think you need to set filename on the image_io. You should provide a filename to the file writer. Take a look at this thread.


Thank you for your reply. I tried that, but I could not get exact ImageIOBase class to work in itk Python. Can you give me an exact example to obtain ImageIOBase::GetNumberOfComponents() in python? or is it not possible?

I got
img = itk.image_file_reader(FileName=filename, ImageIO=itk.GiplImageIO.New())
io = itk.ImageIOBase

data_dimension = io.GetNumberOfDimensions() → I want to do something like these

data_component = io.GetNumberOfComponents()

Hope this helps. (Or should I switch back to simple itk…?)

Thank you

How about:

img = itk.image_file_reader(FileName=filename, ImageIO=io)
data_component = io.GetNumberOfComponents()

Thank you so much Dzenan,

I will try that as soon as possible.
One last question, where do you get all this information? I could not find any documents about this image_file_reader function and itk.GiplImageIO function. Can you give me the link for the documentation or do they just not have one?

You are great, Thanks a lot.

Some of the examples have python versions:

Hi @donghakang ,

Welcome to the ITK Community! :sun_with_face:

Note that with ITK Python, a file can simply be read with standard Python function:

image = itk.imread('./output_filename.hdr')

To get the dimensions:

dimension = image.GetImageDimension()

or the components:

components = image.GetNumberOfComponentsPerPixel()