# Python 3 Skelitonization - plt.show() not work

**URL:** <https://forum.dynamobim.com/t/python-3-skelitonization-plt-show-not-work/63216>\
**Category:** Developers\
**Tags:** cpython3\
**Created:** [April 23, 2021, 12:20pm UTC](https://forum.dynamobim.com/t/python-3-skelitonization-plt-show-not-work/63216 "2021-04-23T12:20:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![oliwer.kulpa](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/oliwer.kulpa/32/105970_2.png) [@oliwer.kulpa](https://forum.dynamobim.com/u/oliwer.kulpa)\
**Post date:** [April 23, 2021, 10:42am UTC](https://forum.dynamobim.com/t/python-3-skelitonization-plt-show-not-work/63216/1 "2021-04-23T10:42:08Z")

</div>

Hi, congratulations on running your sample 🙂  
I’m testing _skimage_ package and I can’t see any window with the results. Should _plt.show()_ run the image window? I can save the processed image to file, but cannot see the window with the result. Any advice? Many thanks in advance 😉

 ![test](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/9/c/9cbbc10aa5901d8737fccb772731672a713479cb.png)

---

<div class="post-metadata">

**Author:** ![SeanP](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/seanp/32/42560_2.png) [@SeanP](https://forum.dynamobim.com/u/SeanP)\
**Post date:** [April 23, 2021, 11:50am UTC](https://forum.dynamobim.com/t/python-3-skelitonization-plt-show-not-work/63216/2 "2021-04-23T11:50:18Z")

</div>

Could you try an Image Watch Node?

---

<div class="post-metadata">

**Author:** ![oliwer.kulpa](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/oliwer.kulpa/32/105970_2.png) [@oliwer.kulpa](https://forum.dynamobim.com/u/oliwer.kulpa)\
**Post date:** [April 23, 2021, 12:11pm UTC](https://forum.dynamobim.com/t/python-3-skelitonization-plt-show-not-work/63216/3 "2021-04-23T12:11:39Z")

</div>

Thanks for getting back to me. Do you mean I could preview what I’ve saved? I can do it - many thanks! But it would be a bit nicer to make _plt.show()_ work. I also tried _plt.imgshow()_ and it’s not working.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/8/8/88cdfd800271fef2c977220250f280d2c8ef3f5a.png)

---

<div class="post-metadata">

**Author:** ![jacob.small](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jacob.small/32/161030_2.png) [@jacob.small](https://forum.dynamobim.com/u/jacob.small)\
**Post date:** [April 23, 2021, 12:20pm UTC](https://forum.dynamobim.com/t/python-3-skelitonization-plt-show-not-work/63216/4 "2021-04-23T12:20:28Z")

</div>

This is more around a particular python library than Python 3 support, so I am moving it to a new thread. 🙂

---

<div class="post-metadata">

**Author:** ![jacob.small](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jacob.small/32/161030_2.png) [@jacob.small](https://forum.dynamobim.com/u/jacob.small)\
**Post date:** [April 23, 2021, 12:25pm UTC](https://forum.dynamobim.com/t/python-3-skelitonization-plt-show-not-work/63216/6 "2021-04-23T12:25:02Z")

</div>

Made this topic public on April 23, 2021, 12:25pm UTC



---

<div class="post-metadata">

**Author:** ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)\
**Post date:** [May 15, 2021, 3:44pm UTC](https://forum.dynamobim.com/t/python-3-skelitonization-plt-show-not-work/63216/7 "2021-05-15T15:44:31Z")

</div>

Hello  
here is a workaround by converting ndarray image to Bitmap (Net Framework), so you can directly use the image in Dynamo

 ![Screenshot_3](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/2/c/2ced9ed9dcbd4ce91ff06a36c0a384bdc38b2c40.jpeg)

```
import sys
import clr
import System
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
sys.path.append(r'C:\Users\sneep\AppData\Local\python-3.8.3-embed-amd64\Lib\site-packages')
import System
clr.AddReference('System.Drawing')
import System.Drawing

from System.Drawing import *
from System.Drawing.Imaging import *

import numpy as np
from scipy import misc
from matplotlib import cm
from matplotlib import colors as mcolors
face = misc.face() #ndarray 3 dimmensions
face2dG = misc.face(gray = True) #ndarray 2 dimmensions

def getRGBfromPalette(npArr, colorMapName):
    """ create a palette color from max value in ndarray """
    color_map = cm.get_cmap(colorMapName)
    a_max = np.amax(npArr)
    palettNp = color_map(np.linspace(0, 1, a_max + 1))
    palettRGB = [(r * 255, g * 255, b * 255) for r,g,b,a in palettNp]
    return palettRGB

def convertToBitmap(npImgArray, colorMapNamefor2D = 'viridis'):
    """ create Bitmap from ndarray image """
    palettRGBfor2D = getRGBfromPalette(npImgArray, colorMapNamefor2D)
    bmpOut = Bitmap(npImgArray.shape[1], npImgArray.shape[0])
    for i in range(npImgArray.shape[1]):
        for j in range(npImgArray.shape[0]):
            if npImgArray.ndim == 3:
                subnpArray = npImgArray[j][i]
                bmpOut.SetPixel(i, j, Color.FromArgb(subnpArray[0], subnpArray[1], subnpArray[2]))
            #    
            elif npImgArray.ndim == 2:
                subnpArray = npImgArray[j][i]
                r, g, b = palettRGBfor2D[subnpArray]
                bmpOut.SetPixel(i, j, Color.FromArgb(r, g, b))
            #    
            else:
                pass
            
    return bmpOut

OUT = convertToBitmap(face), convertToBitmap(face2dG, colorMapNamefor2D = 'gray'), convertToBitmap(face2dG, colorMapNamefor2D = 'inferno')
```

---

<div class="post-metadata">

**Author:** ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)\
**Post date:** [October 11, 2021, 8:42pm UTC](https://forum.dynamobim.com/t/python-3-skelitonization-plt-show-not-work/63216/8 "2021-10-11T20:42:35Z")

</div>

Finally, I found a better (faster) way with Stream conversion, (Bitmap.SetPixel() is too slow)

![waveSinCos](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/3/2/324571fd33a859b4997325e07f4c47efb99be03a.gif)

```auto
import sys
import clr
import System
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
dirAppLoc = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData) 
sys.path.append(dirAppLoc + r'\python-3.8.3-embed-amd64\Lib\site-packages')

clr.AddReference('System.Drawing')
import System.Drawing
from System.Drawing import *
from System.Drawing.Imaging import *
from System.IO import MemoryStream

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import io
import math

def plt2arr(fig):
    """
    need to draw if figure is not drawn yet
    """
    fig.canvas.draw()
    rgba_buf = fig.canvas.buffer_rgba()
    (w,h) = fig.canvas.get_width_height()
    rgba_arr = np.frombuffer(rgba_buf, dtype=np.uint8).reshape((h,w,4))
    return rgba_arr

def convertToBitmap2(npImgArray):
    bitmap_ = None
    # remove alpha
    if npImgArray.ndim == 3 and npImgArray.shape[-1] == 4:
        npImgArray = npImgArray[:, :, :-1]
    # convert to PIL Image
    if npImgArray.ndim == 3:
        image = Image.fromarray(npImgArray, "RGB")
    else:
        image = Image.fromarray(npImgArray, "L")
    # convert to Python ByteArray
    byteIO = io.BytesIO()
    image.save(byteIO, format='BMP')
    byteArr = byteIO.getvalue()
    # convert to Net ByteArray
    netBytes = System.Array[System.Byte](byteArr)
    with MemoryStream(netBytes) as ms:
        bitmap_ = Bitmap(ms)
    return bitmap_

# plot sin and cos wave
fig = plt.figure()
ax = fig.add_subplot(111)
x = np.linspace(-np.pi, np.pi)

ax.set_xlim(-np.pi, np.pi)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.plot(x, np.sin(x), label="sin")
ax.plot(x, np.cos(x ), label="cos")
ax.legend()
ax.set_title("sin(x) and cos(x)")

image_from_plot = plt2arr(fig)
bitmap1 = convertToBitmap2(image_from_plot)

OUT = bitmap1

```

---

<div class="post-metadata">

**Author:** ![RMohareb](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/rmohareb/32/120534_2.png) [@RMohareb](https://forum.dynamobim.com/u/RMohareb)\
**Post date:** [October 12, 2021, 8:29am UTC](https://forum.dynamobim.com/t/python-3-skelitonization-plt-show-not-work/63216/9 "2021-10-12T08:29:56Z")

</div>

@c.poupin you are Creative and amazing. Thank you.

---

<div class="post-metadata">

**Author:** ![chuongmep](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/chuongmep/32/197537_2.png) [@chuongmep](https://forum.dynamobim.com/u/chuongmep)\
**Post date:** [November 2, 2021, 9:38am UTC](https://forum.dynamobim.com/t/python-3-skelitonization-plt-show-not-work/63216/10 "2021-11-02T09:38:22Z")

</div>

Working perfect with plot 3d

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/e/e/eedcec62b485e3a92b0940b3a93c7bfed5d71c15.png)

---

<div class="post-metadata">

**Author:** ![utech.udara](https://avatars.discourse-cdn.com/v4/letter/u/b782af/32.png) [@utech.udara](https://forum.dynamobim.com/u/utech.udara)\
**Post date:** [December 26, 2022, 1:17pm UTC](https://forum.dynamobim.com/t/python-3-skelitonization-plt-show-not-work/63216/11 "2022-12-26T13:17:23Z")

</div>

Thank you very much for your input. Really useful python code !Amazing!
