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;
}
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];
}
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.");
}
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];
}
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];
}
Aggregations