# How to convert a binary vtk file in ascii vtk file using python?

**URL:** <https://discuss.python.org/t/how-to-convert-a-binary-vtk-file-in-ascii-vtk-file-using-python/56145>\
**Category:** Python Help\
**Created:** [June 19, 2024, 1:49pm UTC](https://discuss.python.org/t/how-to-convert-a-binary-vtk-file-in-ascii-vtk-file-using-python/56145 "2024-06-19T13:49:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![damien\_mattei](https://avatars.discourse-cdn.com/v4/letter/d/a8b319/32.png) [@damien\_mattei](https://discuss.python.org/u/damien_mattei)\
**Post date:** [June 19, 2024, 1:49pm UTC](https://discuss.python.org/t/how-to-convert-a-binary-vtk-file-in-ascii-vtk-file-using-python/56145/1 "2024-06-19T13:49:33Z")

</div>

hello,

all is in the title…

regards  
damien

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [June 19, 2024, 2:01pm UTC](https://discuss.python.org/t/how-to-convert-a-binary-vtk-file-in-ascii-vtk-file-using-python/56145/2 "2024-06-19T14:01:44Z")

</div>

By “vtk file” I **guess** you mean a file with data for [this program](https://docs.vtk.org/en/latest/design_documents/VTKFileFormats.html), and by “ascii vtk file” I **guess** you mean one using the XML format. Is that right? What format is the source - the legacy format, or VTKHDF?

If my guesses are wrong, please explain more clearly. In fact, _in general, please explain clearly up front_, and don’t expect that a title is enough information about the problem.

---

<div class="post-metadata">

**Author:** ![damien\_mattei](https://avatars.discourse-cdn.com/v4/letter/d/a8b319/32.png) [@damien\_mattei](https://discuss.python.org/u/damien_mattei)\
**Post date:** [June 19, 2024, 2:31pm UTC](https://discuss.python.org/t/how-to-convert-a-binary-vtk-file-in-ascii-vtk-file-using-python/56145/3 "2024-06-19T14:31:37Z")

</div>

yes it is VTK file from the link you provided.

yes it is in legacy format, so, header is in ASCII and data in binary, here is what it looks like in the beginning because file is more than 6Gb :

the output is with the ‘less’ command under unix.

```python
# vtk DataFile Version 2.0
Magnetic Field from iPIC3D
BINARY
DATASET STRUCTURED_POINTS
DIMENSIONS 960 768 768
ORIGIN 0 0 0
SPACING 0.086027 0.086050 0.086050
POINT_DATA 566231040
VECTORS B float
<B8>M;Q<B8>M;P;<B8>^V^G<B8>L<F7>^H<B8>N^Z<AC>;<B8>^UC<B8>MM\<B8>N<FA><C7>;<B8>^T}<B8>M<A0>U<B8>Oۢ;<B8>^S<B6><B8>M<F7>4<B8>P<BD>;;<B8>^R<EE><B8>NQ <B8>Q<9F><87>;<B8>^R%<B8>N<A3><AD><B8>R<82><8E>;<B8>^Q[<B8>N<F4><EA><B8>Sfh;<B8>^P<90><B8>OC8<B8>TK^R;<B8>^OĸO<A0>ԸU0m;<B8>^N<F6><B8>O永V^V<83>;<B8>^N(<B8>PH3<B8>V<FD>w;<B8>^MX<B8>P<92>U<B8>W<E5>9;<B8>^L<87><B8>P<E4>ǸX<C<CD>y;<B8>

```

on the third line it is specified BINARY and after the header it is in binary:

for the ASCII format , no it is not XML ,it is human readable VTK file like this example truncated:

```python
# vtk DataFile Version 2.0
Cube example
ASCII
DATASET POLYDATA
POINTS 8 float
0.0 0.0 0.0
1.0 0.0 0.0
1.0 1.0 0.0
0.0 1.0 0.0

```

so to answer it is Legacy ASCII in output and Legacy BINARY in input

i thought i had found a solution here:

[https://vtk.org/pipermail/vtkusers/2004-September/026561.html](https://vtk.org/pipermail/vtkusers/2004-September/026561.html)  
…

but it does not works.

I think the solution is to use the vtk python package but the doc is hard to read ,  
i use

`help(vtk.vtkDataSetWriter);`

i will be already happy to have another way to display the python doc?

for a solution i think the vtk library has some method to do that ,perheaps reading the header, and the data separately in binary and using method to output that in ASCII vtk…

i’m still searching but i think someone should have already written this code.

---

<div class="post-metadata">

**Author:** ![damien\_mattei](https://avatars.discourse-cdn.com/v4/letter/d/a8b319/32.png) [@damien\_mattei](https://discuss.python.org/u/damien_mattei)\
**Post date:** [June 19, 2024, 2:56pm UTC](https://discuss.python.org/t/how-to-convert-a-binary-vtk-file-in-ascii-vtk-file-using-python/56145/4 "2024-06-19T14:56:58Z")

</div>

i think i got the solution:

```python
#python3.11 vtk_binary2ascii.py data/Dipole3D_B_6000.vtk data/Dipole3D_B_6000-ASCII.vtk
#/opt/homebrew/var/www/drive/

import vtk
import sys

r = vtk.vtkDataSetReader()
r.SetFileName(sys.argv[1])
r.Update()

data=r.GetOutput()

#print(data)

w = vtk.vtkDataSetWriter()
#w.SetInput( r.GetOutput() )
w.SetInputData( data )
w.SetFileName(sys.argv[2])
w.Write()

```

output

```python
# vtk DataFile Version 5.1
vtk output
ASCII
DATASET STRUCTURED_POINTS
DIMENSIONS 960 768 768
SPACING 0.086027 0.08605 0.08605
ORIGIN 0 0 0
POINT_DATA 566231040
VECTORS B float
-4.89311e-05 -4.8931e-05 0.00561786 -4.88675e-05 -4.91391e-05 0.00561777 -4.89479e-05 -4.93478e-05 0.00561768 
-4.90251e-05 -4.95572e-05 0.00561758 -4.9106e-05 -4.97673e-05 0.00561749 -4.91897e-05 -4.99781e-05 0.0056174 
-4.92667e-05 -5.01895e-05 0.0056173 -4.93423e-05 -5.04017e-05 0.00561721 -4.94152e-05 -5.06147e-05 0.00561711 
-4.95024e-05 -5.08283e-05 0.00561702 -4.95675e-05 -5.10425e-05 0.00561692 -4.96583e-05 -5.12576e-05 0.00561683 

```

but the code is still running and i have no more space on disk because the 6gb file will be 10 times more in ASCII 😱 on my mac laptop…
