# I need a python code to get center line of segmentations

**URL:** <https://discourse.slicer.org/t/i-need-a-python-code-to-get-center-line-of-segmentations/25779>\
**Category:** Development\
**Created:** [October 19, 2022, 3:17pm UTC](https://discourse.slicer.org/t/i-need-a-python-code-to-get-center-line-of-segmentations/25779 "2022-10-19T15:17:51Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![hbr](https://avatars.discourse-cdn.com/v4/letter/h/8edcca/32.png) [@hbr](https://discourse.slicer.org/u/hbr)\
**Post date:** [October 19, 2022, 3:17pm UTC](https://discourse.slicer.org/t/i-need-a-python-code-to-get-center-line-of-segmentations/25779/1 "2022-10-19T15:17:51Z")

</div>

i wanna write a code to get center of seg and dont know how do it. please help

---

<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:** [October 19, 2022, 4:21pm UTC](https://discourse.slicer.org/t/i-need-a-python-code-to-get-center-line-of-segmentations/25779/2 "2022-10-19T16:21:08Z")

</div>

You can use Extract Centerline module logic. See the source code and how it is used from the widget class [here](https://github.com/vmtk/SlicerExtension-VMTK/blob/913e0f9a7a4a085b80d877ff3730e98374b9ee98/ExtractCenterline/ExtractCenterline.py).

---

<div class="post-metadata">

**Author:** ![hbr](https://avatars.discourse-cdn.com/v4/letter/h/8edcca/32.png) [@hbr](https://discourse.slicer.org/u/hbr)\
**Post date:** [October 20, 2022, 3:53am UTC](https://discourse.slicer.org/t/i-need-a-python-code-to-get-center-line-of-segmentations/25779/3 "2022-10-20T03:53:33Z")

</div>

thank you lassoan. i have a problem to install slicer on vs code and test my code and use the widgets class.

Do you have any solution to help me?

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [October 20, 2022, 4:02am UTC](https://discourse.slicer.org/t/i-need-a-python-code-to-get-center-line-of-segmentations/25779/4 "2022-10-20T04:02:43Z")

</div>

As a first step, we suggest you follow the `Tutorials for software developers` available on tutorial pages referenced at [https://slicer.readthedocs.io/en/latest/user\_guide/getting\_started.html#tutorials](https://slicer.readthedocs.io/en/latest/user_guide/getting_started.html#tutorials).

These should help you understand how to create a new module and edit its code.

---

<div class="post-metadata">

**Author:** ![hbr](https://avatars.discourse-cdn.com/v4/letter/h/8edcca/32.png) [@hbr](https://discourse.slicer.org/u/hbr)\
**Post date:** [October 20, 2022, 4:20am UTC](https://discourse.slicer.org/t/i-need-a-python-code-to-get-center-line-of-segmentations/25779/5 "2022-10-20T04:20:36Z")

</div>

thank you jean.

But it is not my problem. I could not run my code in vscode. slicer not recognized true and their mudules not loaded in vscode.

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [October 20, 2022, 5:19am UTC](https://discourse.slicer.org/t/i-need-a-python-code-to-get-center-line-of-segmentations/25779/6 "2022-10-20T05:19:59Z")

</div>

Considering Slicer modules written in python are expected to be discovered and loaded by the Slicer application, you can not “run” the corresponding python code in an editor like `vscode`.

Such code editor is helpful to write the code by leveraging syntax coloring, linting, …

The execution of the corresponding code happen in Slicer itself.

To further understand these concept, I still recommend you review the tutorials I referenced above. More may start by reviewing [this tutorial](https://github.com/PerkLab/PerkLabBootcamp/blob/master/Doc/day3_2_SlicerProgramming.pptx).

---

<div class="post-metadata">

**Author:** ![hbr](https://avatars.discourse-cdn.com/v4/letter/h/8edcca/32.png) [@hbr](https://discourse.slicer.org/u/hbr)\
**Post date:** [October 20, 2022, 5:31am UTC](https://discourse.slicer.org/t/i-need-a-python-code-to-get-center-line-of-segmentations/25779/7 "2022-10-20T05:31:27Z")

</div>

thanks.

i think that i could not specify my problem correctly.

as an example when i import slicer there is not any module in it. there are nothings to use as modules.

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [October 20, 2022, 5:52am UTC](https://discourse.slicer.org/t/i-need-a-python-code-to-get-center-line-of-segmentations/25779/8 "2022-10-20T05:52:59Z")

</div>

Assuming you are running the code in the [python interpreter](https://slicer.readthedocs.io/en/latest/user_guide/user_interface.html#application-menu) available in the application, modules are available in `silcer.modules`.

For example, see [Script repository — 3D Slicer documentation](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#show-color-legend-for-a-volume-node)

To be able to help you more effectively, consider including screenshot to illustrate your problem.

---

<div class="post-metadata">

**Author:** ![hbr](https://avatars.discourse-cdn.com/v4/letter/h/8edcca/32.png) [@hbr](https://discourse.slicer.org/u/hbr)\
**Post date:** [October 20, 2022, 6:00am UTC](https://discourse.slicer.org/t/i-need-a-python-code-to-get-center-line-of-segmentations/25779/9 "2022-10-20T06:00:52Z")

</div>

Exactly my problem is here. `silcer.modules` not working.

 ![Screenshot from 2022-10-20 09-28-49](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/b/7/b79d30d7af6057dce381c7329ef9b80cde33cc15.png)

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [October 20, 2022, 6:08am UTC](https://discourse.slicer.org/t/i-need-a-python-code-to-get-center-line-of-segmentations/25779/10 "2022-10-20T06:08:52Z")

</div>

Since `PythonSlicer` is a launcher starting regular python interpreter, the Slicer modules are not available. These are loaded by the Slicer application.

Instead, you should consider:

- copy/past the code in the Slicer python console
- writing Slicer module (see tutorial above) and then click on a `Reload & Test`

---

<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:** [October 20, 2022, 8:44pm UTC](https://discourse.slicer.org/t/i-need-a-python-code-to-get-center-line-of-segmentations/25779/11 "2022-10-20T20:44:32Z")

</div>

You can also attach Visual Studio code to the Slicer application once the application is running and set up its Python environments. See instructions [here](https://slicer.readthedocs.io/en/latest/developer_guide/debugging/python.html). You can then see all modules, have auto-complete, method documentation, etc.

You can also use Jupyter notebooks, using Slicer as the kernel (using [SlicerJupyter extension](https://github.com/Slicer/SlicerJupyter)).

---

<div class="post-metadata">

**Author:** ![hbr](https://avatars.discourse-cdn.com/v4/letter/h/8edcca/32.png) [@hbr](https://discourse.slicer.org/u/hbr)\
**Post date:** [October 22, 2022, 4:44am UTC](https://discourse.slicer.org/t/i-need-a-python-code-to-get-center-line-of-segmentations/25779/12 "2022-10-22T04:44:57Z")

</div>

thank you Andras

it is very useful for me.
