# Bad saving in MET\_UCHAR instead MET\_UCHAR\_ARRAY

**URL:** https://discourse.vtk.org/t/bad-saving-in-met-uchar-instead-met-uchar-array/7098
**Category:** Support
**Created:** [November 7, 2021, 1:45pm UTC](https://discourse.vtk.org/t/bad-saving-in-met-uchar-instead-met-uchar-array/7098 "2021-11-07T13:45:16Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Luis\_Carlos\_Carneiro](https://discourse.vtk.org/user_avatar/discourse.vtk.org/luis_carlos_carneiro/32/1360_2.png) [@Luis\_Carlos\_Carneiro](https://discourse.vtk.org/u/Luis_Carlos_Carneiro)
#### Post date: [November 7, 2021, 1:45pm UTC](https://discourse.vtk.org/t/bad-saving-in-met-uchar-instead-met-uchar-array/7098/1 "2021-11-07T13:45:16Z")

</div>

Dear All,

I want that the image\_3d be saved as MET\_UCHAR\_ARRAY and the code below saves in MET\_UCHAR with only one component instead of three. How can be instructed the saving be done the right way?

Thanks,

Luís Gonçalves

```
  self.image_3d = vtk.vtkImageData()
    self.image_3d.SetDimensions(dim5)
    self.image_3d.SetSpacing(scale5)
    self.image_3d.AllocateScalars(vtk.VTK_UNSIGNED_CHAR, 3)
    points = self.image_3d.GetPointData()
    points.SetScalars(numpy_support.numpy_to_vtk(
    np.zeros((dim5[0]*dim5[1]*dim5[2]*3), dtype=np.uint8)))

    w = vtk.vtkMetaImageWriter()
    w.SetFileName(path1+file6+".mhd")
    w.SetInputData(self.image_3d)
    w.SetCompression(False)
    w.Write()
```

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [November 7, 2021, 8:13pm UTC](https://discourse.vtk.org/t/bad-saving-in-met-uchar-instead-met-uchar-array/7098/2 "2021-11-07T20:13:16Z")

</div>

> [@Luis\_Carlos\_Carneiro](#):
>
> `np.zeros((dim5[0]*dim5[1]*dim5[2]*3), dtype=np.uint8)))`

The “components” needs to be a separate dimension, try this instead:

```python
np.zeros((dim5[0]*dim5[1]*dim5[2],3), dtype=np.uint8)))

```

You can remove the call to `AllocateScalars()` since `SetScalars()` will simply replace the allocated scalars with the new ones. In other words, setting the data type and number of components with `AllocateScalars()` is pointless if the allocated scalars are immediately replaced with new scalars.
