[vtkusers] Python Import problems (byte order?)

Brian London bml573 at northwestern.edu
Fri May 5 11:49:46 EDT 2006


I am trying to save only a smaller region of a large vtk file in 
python.  The code is below.

Here is the problem though, if I have the line below 
"imArray2=imArray[:,:,:]", then everything works fine.  However, if I do 
"imArray2=imArray[104:134,70:104,70:90]", which is what I am trying to 
do, then the output comes out all jumbled up as if it was written in the 
wrong order.

#!/usr/bin/env python

import vtk

v = vtk.vtkDICOMImageReader()
v.SetFileName("DICOMDIR")
v.SetDirectoryName("01111521")
v.Update()

# Write mri file
w = vtk.vtkDataSetWriter()
w.SetFileName("mri.vtk")
w.SetFileTypeToASCII()
w.SetScalarsName("MRI Data")
w.SetInput(v.GetOutput())
w.Write()

# Find smaller region
from Numeric import *
imArray = zeros((256*256*120,),UnsignedInt16)

e = vtk.vtkImageExport()
e.SetInput(v.GetOutput())
e.Export(imArray)

imArray = reshape(imArray, (256,256,120))
#imArray2 = imArray[104:134,70:104,70:90]
imArray2 = imArray[:,:,:]

i = vtk.vtkImageImport()
i.SetDataScalarTypeToUnsignedShort()

spacing = v.GetOutput().GetSpacing()
i.SetDataSpacing(spacing[0], spacing[1], spacing[2])

dim = imArray2.shape
ext = ( 0, dim[0]-1,
       0, dim[1]-1,
       0, dim[2]-1 )

s = imArray2.tostring()

i.SetWholeExtent(ext)
i.SetDataExtent(ext)
i.SetImportVoidPointer(s,1)
i.Update()

w2 = vtk.vtkDataSetWriter()
w2.SetFileName("mri2.vtk")
w2.SetFileTypeToASCII()
w2.SetScalarsName("MRI Data")
w2.SetInput(i.GetOutput())
w2.Write()



More information about the vtkusers mailing list