Search in sources :

Example 1 with ImageValue

use of org.knime.core.data.image.ImageValue in project knime-core by knime.

the class ImageColumnWriterNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    CheckUtils.checkDestinationDirectory(m_directory.getStringValue());
    final long max = inData[0].size();
    URL remoteBaseUrl = FileUtil.toURL(m_directory.getStringValue());
    Path localDir = FileUtil.resolveToPath(remoteBaseUrl);
    final int colIndex = inData[0].getDataTableSpec().findColumnIndex(m_imageColumn.getStringValue());
    long count = 0;
    long missingCellCount = 0;
    for (DataRow row : inData[0]) {
        exec.checkCanceled();
        exec.setProgress(count++ / (double) max);
        DataCell cell = row.getCell(colIndex);
        if (cell.isMissing()) {
            missingCellCount++;
            getLogger().debug("Skipping row " + row.getKey() + " since the cell is missing.");
        } else {
            ImageValue v = (ImageValue) cell;
            final String ext = v.getImageExtension();
            String name = row.getKey().getString() + "." + ext;
            exec.setProgress(count / (double) max, "Writing " + name + " (" + count + " of " + max + ")");
            Path imageFile = null;
            URL imageUrl = null;
            if (localDir != null) {
                imageFile = PathUtils.resolvePath(localDir, name);
                if (!m_overwrite.getBooleanValue() && Files.exists(imageFile)) {
                    throw new IOException("Output file '" + imageFile + "' exists and must not be overwritten due to user settings");
                }
                // create parent directories in case the row key denotes a path
                Path parentDir = imageFile.getParent();
                if (!Files.isDirectory(parentDir)) {
                    // if parentDir is a symlink pointing to a directory, createDirectories will fail
                    Files.createDirectories(parentDir);
                }
            } else {
                String baseUrl = remoteBaseUrl.toString();
                imageUrl = new URL(baseUrl + (baseUrl.endsWith("/") ? "" : "/") + name);
            }
            ImageContent content = v.getImageContent();
            try (OutputStream os = openOutputStream(imageUrl, imageFile)) {
                content.save(os);
            }
        }
    }
    if (missingCellCount > 0) {
        setWarningMessage("Skipped " + missingCellCount + " row(s) due to missing values.");
    }
    return new BufferedDataTable[0];
}
Also used : Path(java.nio.file.Path) ImageContent(org.knime.core.data.image.ImageContent) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) IOException(java.io.IOException) DataRow(org.knime.core.data.DataRow) ImageValue(org.knime.core.data.image.ImageValue) URL(java.net.URL)

Example 2 with ImageValue

use of org.knime.core.data.image.ImageValue 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 ImageValue

use of org.knime.core.data.image.ImageValue 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 4 with ImageValue

use of org.knime.core.data.image.ImageValue 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)

Aggregations

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 PortObject (org.knime.core.node.port.PortObject)3 ImagePortObject (org.knime.core.node.port.image.ImagePortObject)3 BufferedOutputStream (java.io.BufferedOutputStream)2 OutputStream (java.io.OutputStream)2 URL (java.net.URL)2 Path (java.nio.file.Path)2 DataRow (org.knime.core.data.DataRow)2 PNGImageValue (org.knime.core.data.image.png.PNGImageValue)2 BufferedDataTable (org.knime.core.node.BufferedDataTable)2 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 DataTableSpec (org.knime.core.data.DataTableSpec)1 RowIterator (org.knime.core.data.RowIterator)1