# How to reduce size of the stl file generated through python script?

**URL:** <https://discourse.slicer.org/t/how-to-reduce-size-of-the-stl-file-generated-through-python-script/6414>\
**Category:** Development\
**Created:** [April 5, 2019, 9:58pm UTC](https://discourse.slicer.org/t/how-to-reduce-size-of-the-stl-file-generated-through-python-script/6414 "2019-04-05T21:58:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Siddharth\_Sharma](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/siddharth_sharma/32/3392_2.png) [@Siddharth\_Sharma](https://discourse.slicer.org/u/Siddharth_Sharma)\
**Post date:** [April 5, 2019, 9:58pm UTC](https://discourse.slicer.org/t/how-to-reduce-size-of-the-stl-file-generated-through-python-script/6414/1 "2019-04-05T21:58:23Z")

</div>

i am using [this script](https://gist.github.com/lassoan/1673b25d8e7913cbc245b4f09ed853f9) to generate a stl file.  
i want to reduce the size/quality of the stl file generated through python script. how will i do that?

---

<div class="post-metadata">

**Author:** ![techrama](https://avatars.discourse-cdn.com/v4/letter/t/d07c76/32.png) [@techrama](https://discourse.slicer.org/u/techrama)\
**Post date:** [April 9, 2019, 4:07am UTC](https://discourse.slicer.org/t/how-to-reduce-size-of-the-stl-file-generated-through-python-script/6414/2 "2019-04-09T04:07:19Z")

</div>

for example

```
import numpy
from stl import mesh

# Using an existing stl file:
your_mesh = mesh.Mesh.from_file('some_file.stl')

# Or creating a new mesh (make sure not to overwrite the `mesh` import by
# naming it `mesh`):
VERTICE_COUNT = 100
data = numpy.zeros(VERTICE_COUNT, dtype=mesh.Mesh.dtype)
your_mesh = mesh.Mesh(data, remove_empty_areas=False)

# The mesh normals (calculated automatically)
your_mesh.normals
# The mesh vectors
your_mesh.v0, your_mesh.v1, your_mesh.v2
# Accessing individual points (concatenation of v0, v1 and v2 in triplets)
assert (your_mesh.points[0][0:3] == your_mesh.v0[0]).all()
assert (your_mesh.points[0][3:6] == your_mesh.v1[0]).all()
assert (your_mesh.points[0][6:9] == your_mesh.v2[0]).all()
assert (your_mesh.points[1][0:3] == your_mesh.v0[1]).all()

your_mesh.save('new_stl_file.stl')
```

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 9, 2019, 4:23am UTC](https://discourse.slicer.org/t/how-to-reduce-size-of-the-stl-file-generated-through-python-script/6414/3 "2019-04-09T04:23:55Z")

</div>

@techrama Would you like to tell us what the code snippet above is supposed to do?

@Siddharth_Sharma You can decrease size & quality of the generated mesh by adding this line before `CreateClosedSurfaceRepresentation` method call:

```
segmentationNode.GetSegmentation().SetConversionParameter("Decimation factor", "0.9") 

```

The parameter value is between 0.0-1.0 and it specifies the fraction points that you would like to remove from the mesh.

---

<div class="post-metadata">

**Author:** ![techrama](https://avatars.discourse-cdn.com/v4/letter/t/d07c76/32.png) [@techrama](https://discourse.slicer.org/u/techrama)\
**Post date:** [April 26, 2019, 3:50pm UTC](https://discourse.slicer.org/t/how-to-reduce-size-of-the-stl-file-generated-through-python-script/6414/4 "2019-04-26T15:50:36Z")

</div>

Do you still want me to explain or you got it?

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [April 26, 2019, 4:01pm UTC](https://discourse.slicer.org/t/how-to-reduce-size-of-the-stl-file-generated-through-python-script/6414/5 "2019-04-26T16:01:44Z")

</div>

Source of @techrama 's code can be found at [https://numpy-stl.readthedocs.io/en/latest/usage.html#quickstart](https://numpy-stl.readthedocs.io/en/latest/usage.html#quickstart), if others want to look more into it. It is listed as a “Quickstart” example set.
