documents:120206pyip_cooking:python_imagej_cookbook
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| documents:120206pyip_cooking:python_imagej_cookbook [2025/10/05 22:32] – [Java native 2D Array from Python2D] kota | documents:120206pyip_cooking:python_imagej_cookbook [2025/10/05 22:53] (current) – [Converting Java array types] kota | ||
|---|---|---|---|
| Line 783: | Line 783: | ||
| Here is an example of creating a floating-point image from 2D array. | Here is an example of creating a floating-point image from 2D array. | ||
| - | <code python> | + | <code python |
| from jarray import zeros | from jarray import zeros | ||
| from ij import ImagePlus | from ij import ImagePlus | ||
| Line 806: | Line 806: | ||
| ImagePlus(' | ImagePlus(' | ||
| </ | </ | ||
| + | |||
| + | In short, as simple as: | ||
| + | <code python linenums: | ||
| + | from jarray import zeros | ||
| + | from ij import ImagePlus | ||
| + | from ij.process import FloatProcessor | ||
| + | |||
| + | matrix2D = [zeros(100, ' | ||
| + | ImagePlus(' | ||
| + | </ | ||
| + | |||
| + | |||
| ==== Converting Java array types ==== | ==== Converting Java array types ==== | ||
| - | Sometimes we need to convert the type of Java array e.g. double[] to int[]. | + | Sometimes we need to convert the type of Java array e.g. double[] to int[]. |
| < | < | ||
| + | from ij import IJ | ||
| from ij.process import StackStatistics | from ij.process import StackStatistics | ||
| import jarray | import jarray | ||
| + | imp = IJ.getImage() #stack | ||
| stackstats = StackStatistics(imp) | stackstats = StackStatistics(imp) | ||
| - | histD = stackstats.histogram() | + | histD = stackstats.histogram() |
| - | hist = jarray.zeros(len(histD), ' | + | print(type(histD[0])) |
| - | for i in range(len(histD)): | + | |
| - | hist[i] = int(histD[i]) | + | hist = map(int, histD) |
| + | histInt = jarray.array(hist, ' | ||
| + | print(type(histInt[0])) #Java int[] | ||
| </ | </ | ||
documents/120206pyip_cooking/python_imagej_cookbook.1759703572.txt.gz · Last modified: 2025/10/05 22:32 by kota
