123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- import os
- import re
- import glob
- import numpy as np
- from django.conf import settings
- from skimage.io import imread
- from skimage.io import ImageCollection
- from skimage import img_as_ubyte
- from tifffile import TiffFile
- import subprocess
- import shutil
- import tifffile
- import logging
- logger = logging.getLogger(__name__)
- def processTiffFile(filepath):
- # ends with tif* check
- if re.search('.tif(f)?$', filepath) is None:
- raise ValueError('no tif(f) file given, given filename was "%s"' % filepath)
- logger.debug('tiffPreparer: reading file: "%s"' % filepath)
- inputFile = imread(filepath, plugin='tifffile')
- shape = inputFile.shape
- if len(inputFile.shape) is 2:
- tiffFile = TiffFile(filepath)
- if len(tiffFile.series[0].shape) > 2:
- '''
- skimage could not read tifffile properly
- have to do a workaround:
- 1. let imagej divide the tifffile into multiple images
- 2. let skimage read the multiple images at once
- 3. resize shape to fit the original shape
- '''
- concreteFilenameParts = filepath.split('/')
- concreteFilename = concreteFilenameParts[len(concreteFilenameParts) - 1]
- concreteFilename = re.sub('.tif(f)?$', '', concreteFilename)
- print(concreteFilename)
- sequencePath = settings.PATH_TIFF_SEQUENCER + concreteFilename + '/'
- if os.path.exists(sequencePath):
- shutil.rmtree(sequencePath)
- os.mkdir(sequencePath)
- fJCmd = settings.PATH_FIJI + ' --headless -macro /home/fschultze/master/webapp/imagej/tiffSequencer.txt '
- fJCmd += filepath + ':' + sequencePath + concreteFilename + '.tif'
- print(fJCmd)
- returnCode = subprocess.call(fJCmd, shell=True)
- logger.debug('tiffPreparer: returnCode ' + str(returnCode))
- if returnCode != 0:
- raise Exception('please refer to logs')
- collection = ImageCollection(sequencePath + '*.tif')
- images = collection.concatenate()
- if len(tiffFile.series[0].shape) > len(images.shape):
- images = images.reshape(tiffFile.series[0].shape)
- inputFile = images
- else:
- y, x = shape
- inputFile = inputFile.reshape((1, 1, y, x))
- elif len(inputFile.shape) is 3:
- z, y, x = shape
- inputFile = inputFile.reshape((1, z, y, x))
- return inputFile
- def imread_convert(f):
- image = imread(f, plugin='tifffile')
- return img_as_ubyte(image)
- def prepareImageSequence(path):
- filenames = sorted(glob.glob(os.path.join(path, '*.tif*')))
- z = len(filenames)
- first = tifffile.imread(filenames[0])
- y, x = first.shape
- images = np.empty((z, y, x), dtype=first.dtype)
- images[0, :, :] = first
- for i, filename in enumerate(filenames[1:]):
- images[i, :, :] = tifffile.imread(filename)
- images = images.reshape((1, z, y, x))
- return images
|