tifffile_plugin.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from tifffile import imread as tifffile_imread
  2. from tifffile import imwrite as tifffile_imwrite
  3. __all__ = ['imread', 'imsave']
  4. def imsave(fname, arr, **kwargs):
  5. """Load a tiff image to file.
  6. Parameters
  7. ----------
  8. fname : str or file
  9. File name or file-like object.
  10. arr : ndarray
  11. The array to write.
  12. kwargs : keyword pairs, optional
  13. Additional keyword arguments to pass through (see ``tifffile``'s
  14. ``imwrite`` function).
  15. Notes
  16. -----
  17. Provided by the tifffile library [1]_, and supports many
  18. advanced image types including multi-page and floating-point.
  19. This implementation will set ``photometric='RGB'`` when writing if the first
  20. or last axis of `arr` has length 3 or 4. To override this, explicitly
  21. pass the ``photometric`` kwarg.
  22. This implementation will set ``planarconfig='SEPARATE'`` when writing if the
  23. first axis of arr has length 3 or 4. To override this, explicitly
  24. specify the ``planarconfig`` kwarg.
  25. References
  26. ----------
  27. .. [1] https://pypi.org/project/tifffile/
  28. """
  29. if arr.shape[0] in [3, 4]:
  30. if 'planarconfig' not in kwargs:
  31. kwargs['planarconfig'] = 'SEPARATE'
  32. rgb = True
  33. else:
  34. rgb = arr.shape[-1] in [3, 4]
  35. if rgb and 'photometric' not in kwargs:
  36. kwargs['photometric'] = 'RGB'
  37. return tifffile_imwrite(fname, arr, **kwargs)
  38. def imread(fname, **kwargs):
  39. """Load a tiff image from file.
  40. Parameters
  41. ----------
  42. fname : str or file
  43. File name or file-like-object.
  44. kwargs : keyword pairs, optional
  45. Additional keyword arguments to pass through (see ``tifffile``'s
  46. ``imread`` function).
  47. Notes
  48. -----
  49. Provided by the tifffile library [1]_, and supports many
  50. advanced image types including multi-page and floating point.
  51. References
  52. ----------
  53. .. [1] https://pypi.org/project/tifffile/
  54. """
  55. if 'img_num' in kwargs:
  56. kwargs['key'] = kwargs.pop('img_num')
  57. return tifffile_imread(fname, **kwargs)