pyevtk documentation writearraystovtk 1

pyevtk documentation writearraystovtk

import numpy as np
from enthought.tvtk.api import tvtk, write_data

data = np.random.random((10,10,10))

grid = tvtk.ImageData(spacing=(10, 5, -10), origin=(100, 350, 200), 
grid.point_data.scalars = np.ravel(order='F') = 'Test Data'

# Writes legacy ".vtk" format if filename ends with "vtk", otherwise
# this will write data using the newer xml-based format.
write_data(grid, 'test.vtk')

Here is what the above code is Doing:
1. Create a 10x10x10 array of random numbers.
2. Create a tvtk.ImageData object with the dimensions of the array.
3. Set the scalar data to the array.
4. Write the data to a file.

The ImageData object is a 3D array of voxels. The spacing and origin
attributes are used to define the physical location of the voxels.
The spacing is the distance between voxels in each dimension. The
origin is the physical location of the first voxel. The dimensions
attribute is the number of voxels in each dimension.

The scalar data is stored in the point_data.scalars attribute. The
scalars attribute is a numpy array. The attribute is
used to set the name of the scalar data.

The write_data function is used to write the data to a file. The
first argument is the tvtk object to write. The second argument is
the filename. If the filename ends with “vtk”, then the legacy “.vtk”
format is used. Otherwise, the newer xml-based format is used.

Similar Posts