# Read a VTK from a file content

**URL:** https://discourse.vtk.org/t/read-a-vtk-from-a-file-content/9017
**Category:** Support
**Tags:** python, code, file-formats
**Created:** [July 26, 2022, 5:39am UTC](https://discourse.vtk.org/t/read-a-vtk-from-a-file-content/9017 "2022-07-26T05:39:21Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![moshec](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/ac8455/32.png) [@moshec](https://discourse.vtk.org/u/moshec)
#### Post date: [July 26, 2022, 5:39am UTC](https://discourse.vtk.org/t/read-a-vtk-from-a-file-content/9017/1 "2022-07-26T05:39:21Z")

</div>

Hi everyone,  
I’m using Python,  
I have a tar file that contains some VTK files,  
I don’t want to extract the files to the hard disk and then read the file with a reader-  
Instead, I assign the VTK file content in run time to a local variable, and I’m looking for a way to read it.

This is the method I’m using for reading directly from a VTK file and it works:

```auto
def load_vtk_to_array(path: str):
    reader = vtkStructuredPointsReader()
    reader.SetFileName(path)
    reader.Update()
    data = reader.GetOutput()
    shape = data.GetDimensions()[::-1]
    array = numpy_support.vtk_to_numpy(data.GetPointData().GetArray(0)).reshape(shape)
    return array

```

But I want to use something like this method:

```auto

def load_vtk_to_array(file_content: bytes):
    reader = vtkStructuredPointsReader()
    reader.SetBinaryInputString(file_content)
    reader.Update()
    data = reader.GetOutput() 
    shape = data.GetDimensions()[::-1] ## we get (0,0,0)
    array = numpy_support.vtk_to_numpy(data.GetPointData().GetArray(0)).reshape(shape)  
    return array

```

But this is not work here, the data does not contain GetPointData() method so it raise an exception and the Dimensions we get is (0,0,0)☹

the file headers:

```auto

# vtk DataFile Version 3.0
vtk output
BINARY
DATASET STRUCTURED_POINTS
DIMENSIONS tel:272 102 288
SPACING 1.62 1.62 2.14
ORIGIN 0 0 0
CELL_DATA tel:7855477
POINT_DATA tel:7990272
SCALARS scalars unsigned_short
LOOKUP_TABLE default

```

Thanks in advance

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [July 26, 2022, 1:39pm UTC](https://discourse.vtk.org/t/read-a-vtk-from-a-file-content/9017/2 "2022-07-26T13:39:36Z")

</div>

> [@moshec](#):
>
> I don’t want to extract the files to the hard disk and then read the file with a reader-  
> Instead, I assign the VTK file content in run time to a local variable, and I’m looking for a way to read it.

Not many readers support this mode of operation. Even if they did, there’d need to be some way to hand over a Python object as a stream to the filter via the wrappers (@dgobbi). I made one years ago with `boost::iostreams`, but I’d have to dig it up again (we’d also not want a Boost dependency there anyways).

---

<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: [July 26, 2022, 3:08pm UTC](https://discourse.vtk.org/t/read-a-vtk-from-a-file-content/9017/3 "2022-07-26T15:08:57Z")

</div>

> [@moshec](#):
>
> the data does not contain GetPointData() method

Are you sure about this? Even if the reader fails, a `vtkStructuredPoints` data set _always_ has a `GetPointData()` method.

```plaintext
>>> reader = vtkStructuredPointsReader()
>>> data = reader.GetOutput()
>>> type(data)
<class 'vtkmodules.vtkCommonDataModel.vtkStructuredPoints'>
>>> pd = data.GetPointData()
>>> type(pd)
<class 'vtkmodules.vtkCommonDataModel.vtkPointData'>

```

It’s possible that `SetBinaryInputString()`/`Update()` fails because the Python wrappers encode the binary `char*` as a utf-8 encoded string instead of as binary data.

Have you tried using an ASCII data file with the `SetInputString()` method to see if that works? If an ASCII data file works but a binary data file does not work, then we will know that the `SetBinaryInputString()` method is the problem.

---

<div class="post-metadata">

### Author: ![toddy](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/edb3f5/32.png) [@toddy](https://discourse.vtk.org/u/toddy)
#### Post date: [July 27, 2022, 1:08am UTC](https://discourse.vtk.org/t/read-a-vtk-from-a-file-content/9017/4 "2022-07-27T01:08:41Z")

</div>

This doesn’t look right

```auto
void vtkDataReader::SetBinaryInputString(const char* in, int len)
{
  this->SetInputString(in, len);
}

```

---

<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: [July 27, 2022, 1:19am UTC](https://discourse.vtk.org/t/read-a-vtk-from-a-file-content/9017/5 "2022-07-27T01:19:26Z")

</div>

Since the file header provides information about whether the data is stored in text or in binary, it’s not inconceivable that `SetInputString(in,len)` and `SetBinaryInputString(in,len)` would share the same implementation.

---

<div class="post-metadata">

### Author: ![toddy](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/edb3f5/32.png) [@toddy](https://discourse.vtk.org/u/toddy)
#### Post date: [July 27, 2022, 1:53am UTC](https://discourse.vtk.org/t/read-a-vtk-from-a-file-content/9017/6 "2022-07-27T01:53:27Z")

</div>

> [@toddy](#):
>
> `void vtkDataReader::SetBinaryInputString(const char* in, int len)`

Shouldn’t this be **const unsigned char** \* so that the wrapper code doesn’t convert to utf-8?
