# Python ITK convert mha image to png image

**URL:** https://discourse.itk.org/t/python-itk-convert-mha-image-to-png-image/3060
**Category:** Beginner Questions
**Tags:** python, image
**Created:** [May 12, 2020, 3:42pm UTC](https://discourse.itk.org/t/python-itk-convert-mha-image-to-png-image/3060 "2020-05-12T15:42:03Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jo\_jo](https://discourse.itk.org/user_avatar/discourse.itk.org/jo_jo/32/1458_2.png) [@jo\_jo](https://discourse.itk.org/u/jo_jo)
#### Post date: [May 12, 2020, 3:42pm UTC](https://discourse.itk.org/t/python-itk-convert-mha-image-to-png-image/3060/1 "2020-05-12T15:42:03Z")

</div>

Hello,

I am using itk with python for the first time. I am trying to convert a mha image (2d) to a png image. To be perfectly honest I am not sure how to use pixel types, I hope you can point me into the right direction  
My code looks like this:

```python
        pixelType = itk.F
        imageType = itk.Image[pixelType, 2]
        writerType = itk.ImageFileWriter[imageType]

        reader = itk.ImageFileReader.New(FileName=mha_file)
        input_image_type = itk.template(reader.GetOutput())

        image_filter = itk.CastImageFilter[input_image_type, itk.Image[itk.UC,3]].New(reader.GetOutput())

```

The error I get is quite long:

```nohighlight
 `KeyError: ((<itkTemplate itk::Image>, (<itkCType float>, 3)), <class 'itkImagePython.itkImageUC3'>)

     itkTemplate.TemplateTypeError: itk.CastImageFilter is not wrapped for input type `tuple, itk.Image[itk.UC,3]`.

```

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [May 12, 2020, 6:00pm UTC](https://discourse.itk.org/t/python-itk-convert-mha-image-to-png-image/3060/2 "2020-05-12T18:00:07Z")

</div>

Can you start from [this example](https://itkpythonpackage.readthedocs.io/en/latest/Quick_start_guide.html#basic-examples) from the documentation? This easier syntax is available with ITKv5. In the example you might need to replace median by cast, or you might not need any filter - just read and write the image with different file names.

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [May 14, 2020, 2:22am UTC](https://discourse.itk.org/t/python-itk-convert-mha-image-to-png-image/3060/3 "2020-05-14T02:22:15Z")

</div>

ITK Python can convert image images without type specification with:

```auto
import itk
image = itk.imread('image.mha')
itk.imwrite(image, 'image.png')

```
