# What encoding format does mha file use?

**URL:** <https://discourse.slicer.org/t/what-encoding-format-does-mha-file-use/20933>\
**Category:** Development\
**Created:** [December 6, 2021, 5:07pm UTC](https://discourse.slicer.org/t/what-encoding-format-does-mha-file-use/20933 "2021-12-06T17:07:24Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Junshi\_Peng](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/junshi_peng/32/13393_2.png) [@Junshi\_Peng](https://discourse.slicer.org/u/Junshi_Peng)\
**Post date:** [December 6, 2021, 5:07pm UTC](https://discourse.slicer.org/t/what-encoding-format-does-mha-file-use/20933/1 "2021-12-06T17:07:24Z")

</div>

I am trying to adjusting the header of .mha files, adding transform information into the .mha header by using a python script. But i’m facing a problem finding the correct encoding format to read and write the .mha file, and every time I got invalid output that can neither be uncompressed by VolumeReconstructor command line nor be rendered in 3D Slicer interface. Which encoding format does mha file use? and which one should I use to read and write the .mha file? Great Thank

---

<div class="post-metadata">

**Author:** ![adamrankin](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/adamrankin/32/155_2.png) [@adamrankin](https://discourse.slicer.org/u/adamrankin)\
**Post date:** [December 6, 2021, 5:08pm UTC](https://discourse.slicer.org/t/what-encoding-format-does-mha-file-use/20933/2 "2021-12-06T17:08:35Z")

</div>

Is this a sequence .mha file? Could you post the error(s) that you get?

---

<div class="post-metadata">

**Author:** ![Junshi\_Peng](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/junshi_peng/32/13393_2.png) [@Junshi\_Peng](https://discourse.slicer.org/u/Junshi_Peng)\
**Post date:** [December 7, 2021, 2:47am UTC](https://discourse.slicer.org/t/what-encoding-format-does-mha-file-use/20933/3 "2021-12-07T02:47:58Z")

</div>

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/b/a/bafbf5c06255dec7ab13f2512f7b1fea11ef4628.png)  
Yes, I tried to adjust a .mha file of a png sequence, and after I inserted a header of transform, the .mha became unreadable.  
Here is the screen shot of the .mha file with inserted header, all following the sample data format.  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/d/ed38059c515dfce101acf79e09d1f3da120fdf25.png)

---

<div class="post-metadata">

**Author:** ![Junshi\_Peng](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/junshi_peng/32/13393_2.png) [@Junshi\_Peng](https://discourse.slicer.org/u/Junshi_Peng)\
**Post date:** [December 8, 2021, 1:28am UTC](https://discourse.slicer.org/t/what-encoding-format-does-mha-file-use/20933/4 "2021-12-08T01:28:20Z")

</div>

By the way, does EditSequenceFile.cxx in PlusCommon have the function of reading and editing png sequence? Thank you!

---

<div class="post-metadata">

**Author:** ![adamrankin](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/adamrankin/32/155_2.png) [@adamrankin](https://discourse.slicer.org/u/adamrankin)\
**Post date:** [December 8, 2021, 1:40am UTC](https://discourse.slicer.org/t/what-encoding-format-does-mha-file-use/20933/5 "2021-12-08T01:40:32Z")

</div>

I don’t think it can do png sequence. The pixel data is stored as RGB, either compressed (zip) or uncompressed.

In the past I have first used ImageJ with the MetaImage plugins to load a sequence of images and then write it out into an mha (uncompressed). Then I’ve manually added the transform data into the header with notepad++

---

<div class="post-metadata">

**Author:** ![Junshi\_Peng](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/junshi_peng/32/13393_2.png) [@Junshi\_Peng](https://discourse.slicer.org/u/Junshi_Peng)\
**Post date:** [December 8, 2021, 2:14am UTC](https://discourse.slicer.org/t/what-encoding-format-does-mha-file-use/20933/7 "2021-12-08T02:14:06Z")

</div>

So, can I understand it in this way, that in 3D Slicer, the encoding format of mha is metadata, and only metadata, which can only be edited or added lines by plugins related to itk? Is the notepad++ you’ve mentioned also have itk plugin? Thanks!

---

<div class="post-metadata">

**Author:** ![adamrankin](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/adamrankin/32/155_2.png) [@adamrankin](https://discourse.slicer.org/u/adamrankin)\
**Post date:** [December 8, 2021, 2:25am UTC](https://discourse.slicer.org/t/what-encoding-format-does-mha-file-use/20933/8 "2021-12-08T02:25:23Z")

</div>

mha is metadata and image data. itk can read metadata, but does not recognize a number of tags related to sequences.

notepad++ is just a text editor, which you can use to edit the metadata part of the file.

---

<div class="post-metadata">

**Author:** ![Junshi\_Peng](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/junshi_peng/32/13393_2.png) [@Junshi\_Peng](https://discourse.slicer.org/u/Junshi_Peng)\
**Post date:** [December 8, 2021, 2:36am UTC](https://discourse.slicer.org/t/what-encoding-format-does-mha-file-use/20933/10 "2021-12-08T02:36:14Z")

</div>

I’ve used the PSPad and windows txt editor before, as well as made a simple python script to edit a mha file’s header, but they all returned the error above. It seems not all text editors can do this job, due to the encoding format of mha file, I think.

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [December 8, 2021, 2:51am UTC](https://discourse.slicer.org/t/what-encoding-format-does-mha-file-use/20933/11 "2021-12-08T02:51:05Z")

</div>

It is easier to put a MetaImage into .mhd+.raw files rather than the single .mha file. Then you can edit the metadata in the .mhd file without any issues related to encoding that could corrupt the .mha.

---

<div class="post-metadata">

**Author:** ![Junshi\_Peng](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/junshi_peng/32/13393_2.png) [@Junshi\_Peng](https://discourse.slicer.org/u/Junshi_Peng)\
**Post date:** [December 8, 2021, 3:32am UTC](https://discourse.slicer.org/t/what-encoding-format-does-mha-file-use/20933/12 "2021-12-08T03:32:52Z")

</div>

Yeah this works good. Great thanks
