# converting 3d .zraw image into 2d .png

**URL:** https://discourse.itk.org/t/converting-3d-zraw-image-into-2d-png/3476
**Category:** Uncategorized
**Created:** [September 21, 2020, 3:53pm UTC](https://discourse.itk.org/t/converting-3d-zraw-image-into-2d-png/3476 "2020-09-21T15:53:27Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![abir](https://discourse.itk.org/user_avatar/discourse.itk.org/abir/32/1031_2.png) [@abir](https://discourse.itk.org/u/abir)
#### Post date: [September 21, 2020, 3:53pm UTC](https://discourse.itk.org/t/converting-3d-zraw-image-into-2d-png/3476/1 "2020-09-21T15:53:28Z")

</div>

I used a python code to convert my 3d image into png images but an error occured. Someone suggested me to use Simple itk.  
for every 3d image I should get 3 2d images (axial,coronal and sagittal vue )  
Can any one help me please !

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [September 21, 2020, 4:42pm UTC](https://discourse.itk.org/t/converting-3d-zraw-image-into-2d-png/3476/2 "2020-09-21T16:42:47Z")

</div>

Hello @abir,

This isn’t as trivial as you would think as it depends on the dynamic range of your image and the pixel spacing of your volume. Medical images commonly have a high dynamic range, intensity range much larger than [0,255]. Also medical images are often non-isotropic, the pixel spacing in x, y and z is different. To save a volume as a set of 2D isotropic slices in a low dynamic range format (e.g. png) you will need to deal with these differences.

To map a high dynamic range image to a low dynamic range, you can use [IntensityWindowingImageFilter](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1IntensityWindowingImageFilter.html) or [RescaleIntensityImageFilter](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1RescaleIntensityImageFilter.html#details).

To make an image isotropic see the make\_isotropic function in [this jupyter notebook](https://github.com/InsightSoftwareConsortium/SimpleITK-Notebooks/blob/master/Python/05_Results_Visualization.ipynb). Finally to save all z slices from a volume (change the index from 2 to 0 or 1 to save x or y):

```auto
sitk.WriteImage(image, [os.path.join("slice_{0:03d}.jpg".format(i)) for i in range(image.GetSize()[2])])

```

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)
#### Post date: [September 22, 2020, 5:30am UTC](https://discourse.itk.org/t/converting-3d-zraw-image-into-2d-png/3476/3 "2020-09-22T05:30:08Z")

</div>

Note that it is generally a bad idea to convert a medical image volume to a series 2D slices in jpg/png format. See related discussion in this topic:

> [@Conversion of dicom image format to jpeg or png or bmp](https://discourse.itk.org/t/conversion-of-dicom-image-format-to-jpeg-or-png-or-bmp/3473):
>
> Hi, I am try to get convert a dicom image into jpeg, png or bmp. I would like to know if converting the image array from dicom to jpeg or any other format results loss in image data information? Thanks,Jiten

If you work with medical images, I would recommend to use tools, frameworks, and tutorials developed specifically for medical imaging ([monai](https://monai.io/), [torchio](https://github.com/fepegar/torchio), [NVidia Clara](https://developer.nvidia.com/clara), etc).
