Search in sources :

Example 1 with ImageContent

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

the class ImagePortObject method load.

/**
 * {@inheritDoc}
 */
@Override
protected void load(final PortObjectZipInputStream in, final PortObjectSpec spec, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
    ZipEntry nextEntry = in.getNextEntry();
    String contentClName = nextEntry.getName();
    Class<? extends ImageContent> contentCl;
    try {
        contentCl = (Class<? extends ImageContent>) Class.forName(contentClName);
    } catch (ClassNotFoundException ex) {
        throw new IOException("ImageContent class '" + contentClName + "'" + " does not exist", ex);
    }
    if (!ImageContent.class.isAssignableFrom(contentCl)) {
        throw new IOException("Class '" + contentClName + "' is not an ImageContent");
    }
    Constructor<? extends ImageContent> cons;
    try {
        cons = contentCl.getConstructor(InputStream.class);
    } catch (Exception ex) {
        throw new IOException("ImageContent class '" + contentClName + "' " + "is missing a required constructor, see javadoc", ex);
    }
    try {
        m_content = cons.newInstance(in);
    } catch (Exception ex) {
        throw new IOException("Could not create an instance of '" + contentClName + "'", ex);
    }
    in.close();
    m_spec = (ImagePortObjectSpec) spec;
}
Also used : ImageContent(org.knime.core.data.image.ImageContent) PortObjectZipInputStream(org.knime.core.node.port.PortObjectZipInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException)

Example 2 with ImageContent

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

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

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

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

ImageContent (org.knime.core.data.image.ImageContent)5 DataCell (org.knime.core.data.DataCell)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 IOException (java.io.IOException)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 InputStream (java.io.InputStream)1 ZipEntry (java.util.zip.ZipEntry)1