Skip to main content

ImageJ Region-of-Interest (ROI) Python Tiff exchange module.

Python implementation of the ImageJ ROI API: https://imagej.nih.gov/ij/developer/source/ij/io/RoiDecoder.java.html The underlying code is similar but not the same. Doing things in Python is different to how you would do it in Java. Many of the functions implemented in the Java API are not needed for the Python. The goal is provide coordinates and pixel masks from the main ROI types.

This works with the Christoph Gohlke tifffile module: https://www.lfd.uci.edu/~gohlke/code/tifffile.py.html

This can be installed using pip through: python -m pip install ijroipytiff

Requirements:

  • tifffile
  • numpy

Background:

I wanted a way of efficiently encoding ROI into tiff files so that the information could be used interchangeably between ImageJ and Python.

Example usage Encoder:

from ijroipytiff.ij_roi import Roi
from ijroipytiff.ijpython_encoder import encode_ij_roi,  RGB_encoder
import numpy as np
import tifffile

im_stk = np.zeros((100, 1, 512, 512)).astype(np.float32)


data = []
roi_b = Roi(30, 40, 140, 120, im_stk.shape[2], im_stk.shape[3], 0)
roi_b.name = "Region 1"
roi_b.roiType = 1
roi_b.position = 10
roi_b.strokeLineWidth = 3.0
roi_b.strokeColor = RGB_encoder(255, 0, 255, 255)

data.append(encode_ij_roi(roi_b))

roi_b = Roi(130, 140, 140, 120, im_stk.shape[2], im_stk.shape[3], 0)
roi_b.name = "Region 1"
roi_b.roiType = 1
roi_b.position = 10
roi_b.strokeLineWidth = 3.0
roi_b.strokeColor = RGB_encoder(255, 0, 0, 255)

data.append(encode_ij_roi(roi_b))

metadata = {'hyperstack': True ,'slices': 100, 'channels':1, 'images': 100, 'ImageJ': '1.52g', 'Overlays':data , 'loop': False}

tifffile.imsave("out4.tiff", im_stk, shape=im_stk.shape, imagej=True, ijmetadata=metadata)

Example usage Decoder:

import tifffile
import numpy as np
from ijroipytiff.ij_roi import Roi
from ijroipytiff.ijpython_decoder import decode_ij_roi

from ijroipytiff.ij_ovalroi import OvalRoi
import pylab as plt

pathname2 ="out4.tiff"
tfile = tifffile.TiffFile(pathname2)
img_shape = tfile.asarray().shape


overlay_arr = []
if 'Overlays' in tfile.imagej_metadata:
    overlays = tfile.imagej_metadata['Overlays']
    if overlays.__class__.__name__ == 'list':
        #Multiple overlays and so iterate.
        for overlay in overlays:
            
            overlay_arr.append(decode_ij_roi(overlay,img_shape))
    else:
        #One overlay.
            overlay_arr.append(decode_ij_roi(overlays,img_shape))
else:
    print('no Overlays present in file.')

if 'ROI' in tfile.imagej_metadata:
    print('ROI')
    ROI = tfile.imagej_metadata['ROI']
    decode_ij_roi(ROI,img_shape)
else:
    print("ROI not present in file.")

#Shows how to create mask image.
img = np.zeros((img_shape))
for i in range(0,overlay_arr.__len__()):

    if overlay_arr[i] != False:
        x0 = overlay_arr[i].x
        y0 = overlay_arr[i].y
        wid = overlay_arr[i].width
        hei = overlay_arr[i].height
        img[5,y0:y0+hei, x0:x0+wid] = overlay_arr[i].getMask()
   
    
plt.imshow(img[5,:,:])
plt.show()

Release files for ijroipytiff 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ijroipytiff 0.0.6
File Size Uploaded
ijroipytiff-0.0.6.tar.gz 11.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ijroipytiff 0.0.6
File Interpreter ABI Platform
ijroipytiff-0.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 22.6 kB

Release files / ijroipytiff-0.0.6.tar.gz

Download URL ijroipytiff-0.0.6.tar.gz
Size 11.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ec7dd1c800cf812c4d63657b081972bc2339f3a2117d2cde11a2231ed827ccf2
BLAKE2b-256 checksum
How to use checksums
d9df351cfcecb70f4f4adc6566ba580720e1c8c0bda609944b8bf9b3f17f4f05
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.4

Release files / ijroipytiff-0.0.6-py3-none-any.whl

Download URL ijroipytiff-0.0.6-py3-none-any.whl
Size 11.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
85f439fb87ebbc8f9e02c5a5a3cac2c368dfda1a9694069dc8551f35537df0bf
BLAKE2b-256 checksum
How to use checksums
fa8f32713403d10187cb3a64cf8f533a1398023c2e50e38ae5f25e1054aaa084
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.4

Release history Release notifications | RSS feed

This release

0.0.6 This release

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page