use of org.knime.core.node.port.image.ImagePortObject in project knime-core by knime.
the class WritePNGNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
String outPath = m_fileOutSettings.getStringValue();
ImagePortObject imageObj = (ImagePortObject) inObjects[0];
DataCell imageCellDC = imageObj.toDataCell();
if (!(imageCellDC instanceof ImageValue)) {
throw new InvalidSettingsException("Image object does not produce" + " valid image object but " + imageCellDC == null ? null : imageCellDC.getClass().getName());
}
// overwrite check done in configure()
ImageValue v = (ImageValue) imageCellDC;
ImageContent content = v.getImageContent();
if (content instanceof PNGImageContent) {
byte[] bytes = ((PNGImageContent) content).getByteArrayReference();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
FileOutputStream out = new FileOutputStream(new File(outPath));
FileUtil.copy(in, out);
in.close();
out.close();
} else {
throw new InvalidSettingsException("Unsupported image type: " + content.getClass().getName() + " (expected PNG)");
}
return new PortObject[0];
}
use of org.knime.core.node.port.image.ImagePortObject in project knime-core by knime.
the class TableRowToImageNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
BufferedDataTable inTable = (BufferedDataTable) inObjects[0];
// check for empty table
if (inTable.size() == 0) {
throw new IllegalArgumentException("Input table is empty.");
}
// warn if more than one row
if (inTable.size() > 1) {
setWarningMessage("Input data table has more than one rows! " + "Using first row only.");
}
String column = m_imageColSettingsModel.getStringValue();
DataTableSpec inSpec = inTable.getDataTableSpec();
int columnIndex = inSpec.findColumnIndex(column);
if (columnIndex < 0) {
columnIndex = findImageColumnIndex(inSpec);
}
ImagePortObjectSpec imagePortObjectSpec = new ImagePortObjectSpec(inSpec.getColumnSpec(columnIndex).getType());
final RowIterator it = inTable.iterator();
while (it.hasNext()) {
DataRow row = it.next();
DataCell cell = row.getCell(columnIndex);
if (!cell.isMissing()) {
ImageContent ic = ((ImageValue) cell).getImageContent();
return new PortObject[] { new ImagePortObject(ic, imagePortObjectSpec) };
} else {
setWarningMessage("Found missing image cell, skipping it...");
}
}
throw new IllegalArgumentException("Input table contains only missing cells.");
}
use of org.knime.core.node.port.image.ImagePortObject in project knime-core by knime.
the class ImageToTableNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
ImagePortObject ipo = (ImagePortObject) inObjects[0];
DataTableSpec outspec = createResultSpec(ipo.getSpec(), m_columnNameModel.getStringValue());
BufferedDataContainer buf = exec.createDataContainer(outspec);
RowKey rowKey;
String rowKeyValue = m_rowKeyModel.getStringValue();
if (rowKeyValue == null || rowKeyValue.trim().isEmpty()) {
rowKey = ImageToTableNodeDialog.DEFAULT_ROWKEY;
} else {
rowKey = new RowKey(rowKeyValue);
}
buf.addRowToTable(new DefaultRow(rowKey, ipo.toDataCell()));
buf.close();
buf.getTable();
return new PortObject[] { buf.getTable() };
}
use of org.knime.core.node.port.image.ImagePortObject in project knime-core by knime.
the class WriteImageNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
ImagePortObject imageObj = (ImagePortObject) inObjects[0];
DataCell imageCellDC = imageObj.toDataCell();
if (!(imageCellDC instanceof ImageValue)) {
throw new InvalidSettingsException("Image object does not produce" + " valid image object but " + imageCellDC == null ? null : imageCellDC.getClass().getName());
}
ImageValue v = (ImageValue) imageCellDC;
ImageContent content = v.getImageContent();
final String imageExtension = v.getImageExtension();
String outputLocation = getOutputLocation("." + imageExtension, false);
URL url = FileUtil.toURL(outputLocation);
Path localPath = FileUtil.resolveToPath(url);
try (OutputStream os = openOutputStream(localPath, url)) {
content.save(os);
}
return new PortObject[0];
}
use of org.knime.core.node.port.image.ImagePortObject in project knime-core by knime.
the class DecTreeToImageNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
public PortObject[] execute(final PortObject[] inPorts, final ExecutionContext exec) throws CanceledExecutionException, Exception {
exec.setMessage("Decision Tree To Image: Loading model...");
PMMLPortObject port = (PMMLPortObject) inPorts[0];
List<Node> models = port.getPMMLValue().getModels(PMMLModelType.TreeModel);
if (models.isEmpty()) {
String msg = "Decision Tree evaluation failed: " + "No tree model found.";
LOGGER.error(msg);
throw new RuntimeException(msg);
}
PMMLDecisionTreeTranslator trans = new PMMLDecisionTreeTranslator();
port.initializeModelTranslator(trans);
m_decTree = trans.getDecisionTree();
m_decTree.resetColorInformation();
String colorColumn = null;
if (null != inPorts[1]) {
BufferedDataTable inData = (BufferedDataTable) inPorts[1];
// get column with color information
for (DataColumnSpec s : inData.getDataTableSpec()) {
if (s.getColorHandler() != null) {
colorColumn = s.getName();
break;
}
}
m_decTree.setColorColumn(colorColumn);
for (DataRow thisRow : inData) {
m_decTree.addCoveredColor(thisRow, inData.getDataTableSpec());
}
}
// create PNG via streamed string
ByteArrayOutputStream os = new ByteArrayOutputStream();
int width = m_settings.getWidth();
int height = m_settings.getHeight();
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
BufferedImage image = null;
if (env.isHeadlessInstance()) {
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
} else {
// create compatible image for better performance
GraphicsConfiguration gfxConf = env.getDefaultScreenDevice().getDefaultConfiguration();
// image = gfxConf.createCompatibleImage(width, height);
// with binary transparency
image = gfxConf.createCompatibleImage(width, height, Transparency.BITMASK);
// with transparency
// image = gfxConf.createCompatibleImage(width, height,
// Transparency.TRANSLUCENT);
}
Graphics2D g = (Graphics2D) image.getGraphics();
DecisionTreeNode root = null != getDecisionTree() ? getDecisionTree().getRootNode() : null;
DecTreeGraphView graph = new DecTreeToImageGraphView(root, colorColumn, m_settings);
// draw graph
graph.getView().paint(g);
// write png
ImageIO.write(image, "png", os);
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
m_imageContent = new PNGImageContent(is);
ImagePortObjectSpec outSpec = new ImagePortObjectSpec(PNGImageContent.TYPE);
// return image object
PortObject po = new ImagePortObject(m_imageContent, outSpec);
return new PortObject[] { po };
}
Aggregations