KNIME Image Processing Python Bindings to use ImgPlus in the KNIME Python extension.
KNIME is written in Java while NumPy is a CPython extension. See also:
We use a patched version of tifffile.py by Christoph Goehlke to byte-stream images from and to Python.
This results in an increased memory footprint and additional processing time.
- Python2.7 or Python3.x (not tested)
- NumPy
In order to access the image data from e.g. an "Image Reader" node, a "Python Script" node must contain the following lines:
from KNIPImage import KNIPImage
from scipy import ndimage
# Copy structure of incoming KNIME table
output_table = input_table.copy()
# Create empty output_column
output_column = []
# Loop over every cell in the 'Img' column
for index,input_cell in input_table['Img'].iteritems():
# get image from cell
image = input_cell.array
# apply some operation of image, here a Gaussian filtering
filtered = ndimage.gaussian_filter(image, 3)
# Write result back into a KNIPImage
output_cell = KNIPImage(filtered)
# Append output_cell to output array
output_column.append(output_cell)
# Set output_column in output_table
output_table['Img'] = output_columnNotes:
-
In the above script,
input_cellandoutput_cellare instances of KNIPImage. Further information/metadata could be defined in this class. -
Copying
output_tablefrominput_tablewill keep the table structure that KNIME expects intact. Since we are only interested in manipulating the"Img"column, we can do so without having to worry about creating a new table. -
In the above
copy()operation, arrays are not copied. This is good, because (a) we save memory and (b) we loaded the image from a byte-stream and could not possibly change anything in the previous node. -
Keep in mind that instead of creating an
output_columnand setting it as the"Img"column of theoutput_table, we can just editinput_cell.arrayin-place with e.g.input_cell.array[0,:,:] = 1. This hackish approach saves memory and reduces the amount of scripting code.