feisem.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # -*- coding: utf-8 -*-
  2. # imageio is distributed under the terms of the (new) BSD License.
  3. """Read TIFF from FEI SEM microscopes.
  4. Backend Library: internal
  5. This format is based on :mod:`TIFF <imageio.plugins.tifffile>`, and supports the
  6. same parameters. FEI microscopes append metadata as ASCII text at the end of the
  7. file, which this reader correctly extracts.
  8. Parameters
  9. ----------
  10. discard_watermark : bool
  11. If True (default), discard the bottom rows of the image, which
  12. contain no image data, only a watermark with metadata.
  13. watermark_height : int
  14. The height in pixels of the FEI watermark. The default is 70.
  15. See Also
  16. --------
  17. :mod:`imageio.plugins.tifffile`
  18. """
  19. from .tifffile import TiffFormat
  20. class FEISEMFormat(TiffFormat):
  21. """See :mod:`imageio.plugins.feisem`"""
  22. def _can_write(self, request):
  23. return False # FEI-SEM only supports reading
  24. class Reader(TiffFormat.Reader):
  25. def _get_data(self, index=0, discard_watermark=True, watermark_height=70):
  26. """Get image and metadata from given index.
  27. FEI images usually (always?) contain a watermark at the
  28. bottom of the image, 70 pixels high. We discard this by
  29. default as it does not contain any information not present
  30. in the metadata.
  31. """
  32. im, meta = super(FEISEMFormat.Reader, self)._get_data(index)
  33. if discard_watermark:
  34. im = im[:-watermark_height]
  35. return im, meta
  36. def _get_meta_data(self, index=None):
  37. """Read the metadata from an FEI SEM TIFF.
  38. This metadata is included as ASCII text at the end of the file.
  39. The index, if provided, is ignored.
  40. Returns
  41. -------
  42. metadata : dict
  43. Dictionary of metadata.
  44. """
  45. if hasattr(self, "_fei_meta"):
  46. return self._fei_meta
  47. md = {"root": {}}
  48. current_tag = "root"
  49. reading_metadata = False
  50. filename = self.request.get_local_filename()
  51. with open(filename, encoding="utf8", errors="ignore") as fin:
  52. for line in fin:
  53. if not reading_metadata:
  54. if not line.startswith("Date="):
  55. continue
  56. else:
  57. reading_metadata = True
  58. line = line.rstrip()
  59. if line.startswith("["):
  60. current_tag = line.lstrip("[").rstrip("]")
  61. md[current_tag] = {}
  62. else:
  63. if "=" in line: # ignore empty and irrelevant lines
  64. key, val = line.split("=", maxsplit=1)
  65. for tag_type in (int, float):
  66. try:
  67. val = tag_type(val)
  68. except ValueError:
  69. continue
  70. else:
  71. break
  72. md[current_tag][key] = val
  73. if not md["root"] and len(md) == 1:
  74. raise ValueError("Input file %s contains no FEI metadata." % filename)
  75. self._fei_meta = md
  76. return md