tiffPreparer.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import os
  2. import re
  3. import glob
  4. import numpy as np
  5. from django.conf import settings
  6. from skimage.io import imread
  7. from skimage.io import ImageCollection
  8. from skimage import img_as_ubyte
  9. from tifffile import TiffFile
  10. import subprocess
  11. import shutil
  12. import tifffile
  13. import logging
  14. logger = logging.getLogger(__name__)
  15. def processTiffFile(filepath):
  16. # ends with tif* check
  17. if re.search('.tif(f)?$', filepath) is None:
  18. raise ValueError('no tif(f) file given, given filename was "%s"' % filepath)
  19. logger.debug('tiffPreparer: reading file: "%s"' % filepath)
  20. inputFile = imread(filepath, plugin='tifffile')
  21. shape = inputFile.shape
  22. if len(inputFile.shape) is 2:
  23. tiffFile = TiffFile(filepath)
  24. if len(tiffFile.series[0].shape) > 2:
  25. '''
  26. skimage could not read tifffile properly
  27. have to do a workaround:
  28. 1. let imagej divide the tifffile into multiple images
  29. 2. let skimage read the multiple images at once
  30. 3. resize shape to fit the original shape
  31. '''
  32. concreteFilenameParts = filepath.split('/')
  33. concreteFilename = concreteFilenameParts[len(concreteFilenameParts) - 1]
  34. concreteFilename = re.sub('.tif(f)?$', '', concreteFilename)
  35. print(concreteFilename)
  36. sequencePath = settings.PATH_TIFF_SEQUENCER + concreteFilename + '/'
  37. if os.path.exists(sequencePath):
  38. shutil.rmtree(sequencePath)
  39. os.mkdir(sequencePath)
  40. fJCmd = settings.PATH_FIJI + ' --headless -macro /home/fschultze/master/webapp/imagej/tiffSequencer.txt '
  41. fJCmd += filepath + ':' + sequencePath + concreteFilename + '.tif'
  42. print(fJCmd)
  43. returnCode = subprocess.call(fJCmd, shell=True)
  44. logger.debug('tiffPreparer: returnCode ' + str(returnCode))
  45. if returnCode != 0:
  46. raise Exception('please refer to logs')
  47. collection = ImageCollection(sequencePath + '*.tif')
  48. images = collection.concatenate()
  49. if len(tiffFile.series[0].shape) > len(images.shape):
  50. images = images.reshape(tiffFile.series[0].shape)
  51. inputFile = images
  52. else:
  53. y, x = shape
  54. inputFile = inputFile.reshape((1, 1, y, x))
  55. elif len(inputFile.shape) is 3:
  56. z, y, x = shape
  57. inputFile = inputFile.reshape((1, z, y, x))
  58. return inputFile
  59. def imread_convert(f):
  60. image = imread(f, plugin='tifffile')
  61. return img_as_ubyte(image)
  62. def prepareImageSequence(path):
  63. filenames = sorted(glob.glob(os.path.join(path, '*.tif*')))
  64. z = len(filenames)
  65. first = tifffile.imread(filenames[0])
  66. y, x = first.shape
  67. images = np.empty((z, y, x), dtype=first.dtype)
  68. images[0, :, :] = first
  69. for i, filename in enumerate(filenames[1:]):
  70. images[i, :, :] = tifffile.imread(filename)
  71. images = images.reshape((1, z, y, x))
  72. return images