Skip to content

Instantly share code, notes, and snippets.

@nikitinvv
Created December 13, 2022 21:14
Show Gist options
  • Save nikitinvv/87607be1b08fb3811b7fb9e41d7d258f to your computer and use it in GitHub Desktop.
Save nikitinvv/87607be1b08fb3811b7fb9e41d7d258f to your computer and use it in GitHub Desktop.
# real-time visualization of numpy arrays in ImageJ
######################################################################################
# Installation:
# 1. conda create -n pyimagej -c conda-forge pyimagej openjdk=8
# 2. conda activate pyimagej
#######################################################################################
import imagej
import numpy as np
import time
import scyjava as sj
ij = imagej.init('/home/beams/USER2BMB/Software/Fiji.app',mode='interactive')
ij.ui().showUI()
ImagePlus = sj.jclass('ij.ImagePlus')
#initialize a window
array1 = np.random.rand(32, 32)
image1 = ij.py.to_java(array1)
ij.ui().show(image1, cmap='gray')
imp1 = ij.py.active_imageplus()
while True:
time.sleep(1)
#initialize a new ImagePlus for new data
array2 = np.random.rand(32, 32)
array2[16,:] = 0
array2[:,16] = 0
imp2 = ij.py.to_java(array2)
imp2 = ij.dataset().create(imp2)
imp2 = ij.convert().convert(imp2, ImagePlus)
#place the new ImagePlus in original ImagePlus
imp1.setImage(imp2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment