Python 3 Skelitonization - plt.show() not work

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

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')
8 Likes