Source code for node_figure_to_image
# This file is part of Sympathy for Data.
# Copyright (c) 2018, Combine Control Systems AB
#
# SYMPATHY FOR DATA COMMERCIAL LICENSE
# You should have received a link to the License with Sympathy for Data.
import numpy as np
from matplotlib.backends.backend_agg import FigureCanvasAgg
from sympathy.api import node
from sympathy.api.nodeconfig import Port, Ports, Tag, Tags
from sylib.imageprocessing.image import ImagePort
[docs]
class FigureToImage(node.Node):
author = 'Mathias Broxvall'
icon = 'image_figure_to_image.svg'
description = 'Converts a figure object to an image'
tags = Tags(Tag.ImageProcessing.IO)
name = 'Figure to image'
nodeid = 'com.sympathyfordata.imageanalysis.figure_to_image'
__doc__ = description
inputs = Ports([
Port.Custom('figure', 'Input figure', name='figure'),
])
outputs = Ports([
ImagePort('Result', name='result'),
])
parameters = node.parameters()
parameters.set_float(
'width', value=6,
description='Width of image, measured in inches',
label='Width (inches)'
)
parameters.set_float(
'height', value=4,
description='Height of image, measured in inches',
label='Height (inches)'
)
parameters.set_float(
'dpi', value=120,
description=(
'Number of dots (pixels) per inch, also affects relationship '
'between fontsizes and rendered pixels'),
label='Dots per inch (dpi)'
)
def execute(self, node_context):
params = node_context.parameters
dpi = params['dpi'].value
width = params['width'].value
height = params['height'].value
figure_obj = node_context.input['figure']
fig = figure_obj.get_mpl_figure()
fig.set_dpi(dpi)
fig.set_size_inches((width, height))
fig.canvas = FigureCanvasAgg(fig)
fig.canvas.draw()
im = np.array(fig.canvas.renderer._renderer)
node_context.output['result'].set_image(im)