#!/usr/bin/env python # coding: utf-8 # # ImageJ in Python # # This notebook shows how to use ImageJ as a Python library and uses the OMERO Python API to connect to an OMERO server. # # We load a Zarr image from a public S3 repository # # In this setup, Fiji has already been installed. # We will use the [pyimagej](https://pypi.org/project/pyimagej/) library to access to the entire ImageJ API from Python. # ## Install dependencies # In[1]: get_ipython().run_line_magic('conda', 'install dask zarr') # ## Packages # In[2]: import numpy import dask.array as da import zarr from dask.diagnostics import ProgressBar import imagej # ## Starting ImageJ from Python # In[3]: ij = imagej.init('/srv/conda/vnc/Fiji.app') ij.getVersion() # In[4]: image_id = 6001240 # In[5]: def load_binary_from_s3(id, resolution='0'): endpoint_url = 'https://uk1s3.embassy.ebi.ac.uk/' root = 'idr/zarr/v0.1/%s.zarr/%s/' % (id, resolution) data = da.from_zarr(endpoint_url + root) with ProgressBar(): return numpy.asarray(da.from_zarr(endpoint_url + root)) img = load_binary_from_s3(image_id) print(img.shape) # ## Display image using `ij.py.show()` # In[6]: img_new = numpy.mean(img, axis=2) print(img_new.shape) ij.py.show(img_new[0, 0, :, :], cmap = 'gray') # ## Process numpy arrays in ImageJ # # We use the method `to_java()` to convert into ImageJ types. # In[8]: img = img_new[0, 0, :, :] result = numpy.zeros(img.shape) sigma1 = 8 sigma2 = 2 # note the use of to_java on img and result to turn the numpy images into RAIs ij.op().filter().dog( ij.py.to_java(result), ij.py.to_java(img), sigma1, sigma2) # purple highlights the edges of the vessels, green highlights the centers ij.py.show(result, cmap = 'PRGn') # ### License (BSD 2-Clause) # Copyright (c) 2021, University of Dundee All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: # # Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.