# pvpython script to convert binary Legacy VTK file to some compressed version to reduce file size

**URL:** https://discourse.paraview.org/t/pvpython-script-to-convert-binary-legacy-vtk-file-to-some-compressed-version-to-reduce-file-size/3595
**Category:** ParaView Support
**Tags:** python, vtk
**Created:** [February 18, 2020, 8:44pm UTC](https://discourse.paraview.org/t/pvpython-script-to-convert-binary-legacy-vtk-file-to-some-compressed-version-to-reduce-file-size/3595 "2020-02-18T20:44:00Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![vrkssai](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/v/e19b73/32.png) [@vrkssai](https://discourse.paraview.org/u/vrkssai)
#### Post date: [February 18, 2020, 8:44pm UTC](https://discourse.paraview.org/t/pvpython-script-to-convert-binary-legacy-vtk-file-to-some-compressed-version-to-reduce-file-size/3595/1 "2020-02-18T20:44:00Z")

</div>

I’m looking for a simple script options like:

```auto
from paraview.simple import *
r = LegacyVTKReader( FileNames=['Yourfilename.vtk'] )
SaveData('Yourfilename_binary.vtk', proxy=r)

```

used from: [https://stackoverflow.com/a/58209834](https://stackoverflow.com/a/58209834)

I was wondering if there are options to compress/convert the file to some better version(like \*vtu).

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.paraview.org/u/ben.boeckel)
#### Post date: [March 21, 2020, 1:31am UTC](https://discourse.paraview.org/t/pvpython-script-to-convert-binary-legacy-vtk-file-to-some-compressed-version-to-reduce-file-size/3595/2 "2020-03-21T01:31:41Z")

</div>

Cc: @mwestphal

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [March 23, 2020, 1:24am UTC](https://discourse.paraview.org/t/pvpython-script-to-convert-binary-legacy-vtk-file-to-some-compressed-version-to-reduce-file-size/3595/3 "2020-03-23T01:24:07Z")

</div>

`SaveData('Yourfilename_binary.vtu', proxy=r)`

---

<div class="post-metadata">

### Author: ![vrkssai](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/v/e19b73/32.png) [@vrkssai](https://discourse.paraview.org/u/vrkssai)
#### Post date: [March 23, 2020, 8:42pm UTC](https://discourse.paraview.org/t/pvpython-script-to-convert-binary-legacy-vtk-file-to-some-compressed-version-to-reduce-file-size/3595/4 "2020-03-23T20:42:49Z")

</div>

I’ve tried this but it doesn’t reduce the file size. For example, how to use the LZMA compressor like we would have in the GUI dialog box, to get the smallest file size.  
my obs:  
data.vtk - 2.4Mb  
data.vtu- 2.5Mb  
data.vtu- 1.2Mb(from paraview GUI - LZMA compressor)  
I would like to use something like the last one to drastically reduce the file size. Even converting to other format is also fine with me(I need the format to have Arbitary order Lagrange Hexahedron support), but the objective is to reduce file size.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [March 24, 2020, 1:31am UTC](https://discourse.paraview.org/t/pvpython-script-to-convert-binary-legacy-vtk-file-to-some-compressed-version-to-reduce-file-size/3595/5 "2020-03-24T01:31:18Z")

</div>

I missed the file size reduction part.

```
 from paraview.simple import *
 r = LegacyVTKReader( FileNames=['Yourfilename.vtk'] )
 w = XMLUnstructuredGridWriter(r)
 w.FileName = "Yourcompressedfilename.vtk"
 w.CompressorType = "LZMA"
 w.CompressionLevel = 9
 w.UpdatePipeline()
```

---

<div class="post-metadata">

### Author: ![vrkssai](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/v/e19b73/32.png) [@vrkssai](https://discourse.paraview.org/u/vrkssai)
#### Post date: [April 26, 2020, 3:08am UTC](https://discourse.paraview.org/t/pvpython-script-to-convert-binary-legacy-vtk-file-to-some-compressed-version-to-reduce-file-size/3595/6 "2020-04-26T03:08:52Z")

</div>

I get an error, when i write the script in a file named ‘convert.py’ and run it with ‘pvpython convert.py’:  
Traceback (most recent call last):  
File “convert.py”, line 5, in   
w.CompressorType = LZMA  
NameError: name ‘LZMA’ is not defined

---

<div class="post-metadata">

### Author: ![jourdain](https://discourse.paraview.org/user_avatar/discourse.paraview.org/jourdain/32/11_2.png) [@jourdain](https://discourse.paraview.org/u/jourdain)
#### Post date: [April 26, 2020, 2:18pm UTC](https://discourse.paraview.org/t/pvpython-script-to-convert-binary-legacy-vtk-file-to-some-compressed-version-to-reduce-file-size/3595/7 "2020-04-26T14:18:58Z")

</div>

LZMA variable name that does not exist. But you want to set the CompressorType to the String ‘LZMA’.  
You are just missing the quotes.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [April 27, 2020, 1:12am UTC](https://discourse.paraview.org/t/pvpython-script-to-convert-binary-legacy-vtk-file-to-some-compressed-version-to-reduce-file-size/3595/8 "2020-04-27T01:12:16Z")

</div>

Indeed, I’ve edited my post.

@vrkssai : In doubt, use the python trace method.

---

<div class="post-metadata">

### Author: ![vrkssai](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/v/e19b73/32.png) [@vrkssai](https://discourse.paraview.org/u/vrkssai)
#### Post date: [April 27, 2020, 2:05am UTC](https://discourse.paraview.org/t/pvpython-script-to-convert-binary-legacy-vtk-file-to-some-compressed-version-to-reduce-file-size/3595/9 "2020-04-27T02:05:07Z")

</div>

I don’t understand the ‘python trace method’ part.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [April 27, 2020, 2:28am UTC](https://discourse.paraview.org/t/pvpython-script-to-convert-binary-legacy-vtk-file-to-some-compressed-version-to-reduce-file-size/3595/10 "2020-04-27T02:28:11Z")

</div>

> [@How to figure out a python code using the python trace mechanism](https://discourse.paraview.org/t/how-to-figure-out-a-python-code-using-the-python-trace-mechanism/4186):
>
> Starting to use the python shell and pvpython can be overwhelming sometimes as it can be hard to figure out how to implement what you are looking to do using only python, even with the [documentation](https://kitware.github.io/paraview-docs/latest/python/). Here is a simple solution, the “python trace” mechanism. As its name indicate, the python trace let you trace your action in the UI to create a python script. Here is how I’d recommend to use it. Write your pvpython code until you reach a point of not knowing how to do a specific task in python…
