converting 3d .zraw image into 2d .png

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 !

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 or RescaleIntensityImageFilter.

To make an image isotropic see the make_isotropic function in this jupyter notebook. Finally to save all z slices from a volume (change the index from 2 to 0 or 1 to save x or y):

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

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:

If you work with medical images, I would recommend to use tools, frameworks, and tutorials developed specifically for medical imaging (monai, torchio, NVidia Clara, etc).

1 Like