Skip to main content

Python library for reading and writing ImageJ (or Fiji) ROI and storing them in IJ Tiff format, compatible with Tifffile library.

Project description

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()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ijroipytiff-0.0.6.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

ijroipytiff-0.0.6-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file ijroipytiff-0.0.6.tar.gz.

File metadata

  • Download URL: ijroipytiff-0.0.6.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for ijroipytiff-0.0.6.tar.gz
Algorithm Hash digest
SHA256 ec7dd1c800cf812c4d63657b081972bc2339f3a2117d2cde11a2231ed827ccf2
MD5 27439484c5fc13468851d7bfe8d3e5d3
BLAKE2b-256 d9df351cfcecb70f4f4adc6566ba580720e1c8c0bda609944b8bf9b3f17f4f05

See more details on using hashes here.

File details

Details for the file ijroipytiff-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: ijroipytiff-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for ijroipytiff-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 85f439fb87ebbc8f9e02c5a5a3cac2c368dfda1a9694069dc8551f35537df0bf
MD5 3c748ccdf08387b69e2b7181b967efa9
BLAKE2b-256 fa8f32713403d10187cb3a64cf8f533a1398023c2e50e38ae5f25e1054aaa084

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page