Python <> imagej communication (inspired by #i2k2020 tutorial)

Dear all,

it is a continuation of a discussion which started on @ctrueden workshop on #i2k2020. I move it here since it might be useful to others.

My aim is to develop code examples (snippets) that would allow me to simplify old legacy code where I mix shell / python / .ijm. I would like to get rid of the shell, write in python but still execute .ijm macros inside. To achieve this goal I need to have access to the data from both ends: python <> imagej and I need to run processing on both ends: python <> imagej. For this purpose, I created a short python notebook which I will try to debug and extend by asking for some help in this thread. I actually do not care much at this point for efficiency of code, I rather need something that works first, later I will check how to optimize it.

If you are interested and would like to reproduce the code in this thread, please follow these install instructions for the environment: You can also check Curtis video from #i2k2020 when it will be available.

An important file from which I inspire with conversions is here:

Another one, where macros are covered is here:

The initial file I am going to expand and correct, showing how to load & process data on both ends is here:

Now my current issues and questions that I would like to address:

  1. In the first part of the file (“Loading image via FIJI, processing with .ijm macro, displaying via FIJI”) I try to make a copy of an active image in FIJI. For this purpose I use deepcopy, but I guess there are some problems with that. Any recommended way to do it? I simply need to store intermediate images from fiji in some python variables. The issue is raised when i try to display the stored image with copy_img_ij )

  2. In the second part of the file (" Loading image via skimage, processing with .ijm macro, displaying via FIJI") I read the image in python using io.imread, I convert it and put it to FIJI. Everything seems to work. However when I process the macro, exactly the same way it works in the first part of the tutorial, I do not see visible effects. My feeling is that I do not understand how WindowManager.setTempCurrentImage( imp_via_convert ) really works. Any help with correcting it would be appreciated.

  3. Now after running the second part, if I re-run the first part again, my macro processing does not yield any results. This needs to be some nasty side effect of the last part, still I do not know what is happening and I would need some help here.

That’s it for now. I will continue this investigation when I get some more hints. Thanks a lot!