Search in sources :

Example 1 with ImagePortObject

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];
}
Also used : PNGImageContent(org.knime.core.data.image.png.PNGImageContent) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) PNGImageContent(org.knime.core.data.image.png.PNGImageContent) ImageContent(org.knime.core.data.image.ImageContent) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) DataCell(org.knime.core.data.DataCell) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) ImagePortObject(org.knime.core.node.port.image.ImagePortObject) PNGImageValue(org.knime.core.data.image.png.PNGImageValue) ImageValue(org.knime.core.data.image.ImageValue) File(java.io.File) PortObject(org.knime.core.node.port.PortObject) ImagePortObject(org.knime.core.node.port.image.ImagePortObject)

Example 2 with ImagePortObject

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.");
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) ImagePortObjectSpec(org.knime.core.node.port.image.ImagePortObjectSpec) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) ImagePortObject(org.knime.core.node.port.image.ImagePortObject) DataRow(org.knime.core.data.DataRow) ImageContent(org.knime.core.data.image.ImageContent) RowIterator(org.knime.core.data.RowIterator) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) ImageValue(org.knime.core.data.image.ImageValue) PortObject(org.knime.core.node.port.PortObject) ImagePortObject(org.knime.core.node.port.image.ImagePortObject)

Example 3 with ImagePortObject

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() };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) RowKey(org.knime.core.data.RowKey) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) ImagePortObject(org.knime.core.node.port.image.ImagePortObject) DefaultRow(org.knime.core.data.def.DefaultRow) PortObject(org.knime.core.node.port.PortObject) ImagePortObject(org.knime.core.node.port.image.ImagePortObject)

Example 4 with ImagePortObject

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];
}
Also used : Path(java.nio.file.Path) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ImageContent(org.knime.core.data.image.ImageContent) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) DataCell(org.knime.core.data.DataCell) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) ImagePortObject(org.knime.core.node.port.image.ImagePortObject) PNGImageValue(org.knime.core.data.image.png.PNGImageValue) ImageValue(org.knime.core.data.image.ImageValue) PortObject(org.knime.core.node.port.PortObject) ImagePortObject(org.knime.core.node.port.image.ImagePortObject) URL(java.net.URL)

Example 5 with ImagePortObject

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 };
}
Also used : PNGImageContent(org.knime.core.data.image.png.PNGImageContent) PMMLDecisionTreeTranslator(org.knime.base.node.mine.decisiontree2.PMMLDecisionTreeTranslator) ImagePortObjectSpec(org.knime.core.node.port.image.ImagePortObjectSpec) DecisionTreeNode(org.knime.base.node.mine.decisiontree2.model.DecisionTreeNode) Node(org.w3c.dom.Node) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GraphicsEnvironment(java.awt.GraphicsEnvironment) ImagePortObject(org.knime.core.node.port.image.ImagePortObject) DataRow(org.knime.core.data.DataRow) BufferedImage(java.awt.image.BufferedImage) GraphicsConfiguration(java.awt.GraphicsConfiguration) Graphics2D(java.awt.Graphics2D) DataColumnSpec(org.knime.core.data.DataColumnSpec) DecTreeGraphView(org.knime.base.node.mine.decisiontree2.view.DecTreeGraphView) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedDataTable(org.knime.core.node.BufferedDataTable) PortObject(org.knime.core.node.port.PortObject) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) ImagePortObject(org.knime.core.node.port.image.ImagePortObject) DecisionTreeNode(org.knime.base.node.mine.decisiontree2.model.DecisionTreeNode)

Aggregations

PortObject (org.knime.core.node.port.PortObject)6 ImagePortObject (org.knime.core.node.port.image.ImagePortObject)6 DataCell (org.knime.core.data.DataCell)4 ImageContent (org.knime.core.data.image.ImageContent)4 ImageValue (org.knime.core.data.image.ImageValue)4 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)4 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 DataRow (org.knime.core.data.DataRow)2 DataTableSpec (org.knime.core.data.DataTableSpec)2 PNGImageContent (org.knime.core.data.image.png.PNGImageContent)2 PNGImageValue (org.knime.core.data.image.png.PNGImageValue)2 BufferedDataTable (org.knime.core.node.BufferedDataTable)2 ImagePortObjectSpec (org.knime.core.node.port.image.ImagePortObjectSpec)2 FileStoreURIPortObject (com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject)1 Graphics2D (java.awt.Graphics2D)1 GraphicsConfiguration (java.awt.GraphicsConfiguration)1 GraphicsEnvironment (java.awt.GraphicsEnvironment)1 BufferedImage (java.awt.image.BufferedImage)1 BufferedOutputStream (java.io.BufferedOutputStream)1