example.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # -*- coding: utf-8 -*-
  2. # imageio is distributed under the terms of the (new) BSD License.
  3. """Example plugin. You can use this as a template for your own plugin."""
  4. import numpy as np
  5. from .. import formats
  6. from ..core import Format
  7. class DummyFormat(Format):
  8. """The dummy format is an example format that does nothing.
  9. It will never indicate that it can read or write a file. When
  10. explicitly asked to read, it will simply read the bytes. When
  11. explicitly asked to write, it will raise an error.
  12. This documentation is shown when the user does ``help('thisformat')``.
  13. Parameters for reading
  14. ----------------------
  15. Specify arguments in numpy doc style here.
  16. Parameters for saving
  17. ---------------------
  18. Specify arguments in numpy doc style here.
  19. """
  20. def _can_read(self, request):
  21. # This method is called when the format manager is searching
  22. # for a format to read a certain image. Return True if this format
  23. # can do it.
  24. #
  25. # The format manager is aware of the extensions and the modes
  26. # that each format can handle. It will first ask all formats
  27. # that *seem* to be able to read it whether they can. If none
  28. # can, it will ask the remaining formats if they can: the
  29. # extension might be missing, and this allows formats to provide
  30. # functionality for certain extensions, while giving preference
  31. # to other plugins.
  32. #
  33. # If a format says it can, it should live up to it. The format
  34. # would ideally check the request.firstbytes and look for a
  35. # header of some kind.
  36. #
  37. # The request object has:
  38. # request.filename: a representation of the source (only for reporting)
  39. # request.firstbytes: the first 256 bytes of the file.
  40. # request.mode[0]: read or write mode
  41. if request.extension in self.extensions:
  42. return True
  43. def _can_write(self, request):
  44. # This method is called when the format manager is searching
  45. # for a format to write a certain image. It will first ask all
  46. # formats that *seem* to be able to write it whether they can.
  47. # If none can, it will ask the remaining formats if they can.
  48. #
  49. # Return True if the format can do it.
  50. # In most cases, this code does suffice:
  51. if request.extension in self.extensions:
  52. return True
  53. # -- reader
  54. class Reader(Format.Reader):
  55. def _open(self, some_option=False, length=1):
  56. # Specify kwargs here. Optionally, the user-specified kwargs
  57. # can also be accessed via the request.kwargs object.
  58. #
  59. # The request object provides two ways to get access to the
  60. # data. Use just one:
  61. # - Use request.get_file() for a file object (preferred)
  62. # - Use request.get_local_filename() for a file on the system
  63. self._fp = self.request.get_file()
  64. self._length = length # passed as an arg in this case for testing
  65. self._data = None
  66. def _close(self):
  67. # Close the reader.
  68. # Note that the request object will close self._fp
  69. pass
  70. def _get_length(self):
  71. # Return the number of images. Can be np.inf
  72. return self._length
  73. def _get_data(self, index):
  74. # Return the data and meta data for the given index
  75. if index >= self._length:
  76. raise IndexError("Image index %i > %i" % (index, self._length))
  77. # Read all bytes
  78. if self._data is None:
  79. self._data = self._fp.read()
  80. # Put in a numpy array
  81. im = np.frombuffer(self._data, "uint8")
  82. im.shape = len(im), 1
  83. # Return array and dummy meta data
  84. return im, {}
  85. def _get_meta_data(self, index):
  86. # Get the meta data for the given index. If index is None, it
  87. # should return the global meta data.
  88. return {} # This format does not support meta data
  89. # -- writer
  90. class Writer(Format.Writer):
  91. def _open(self, flags=0):
  92. # Specify kwargs here. Optionally, the user-specified kwargs
  93. # can also be accessed via the request.kwargs object.
  94. #
  95. # The request object provides two ways to write the data.
  96. # Use just one:
  97. # - Use request.get_file() for a file object (preferred)
  98. # - Use request.get_local_filename() for a file on the system
  99. self._fp = self.request.get_file()
  100. def _close(self):
  101. # Close the reader.
  102. # Note that the request object will close self._fp
  103. pass
  104. def _append_data(self, im, meta):
  105. # Process the given data and meta data.
  106. raise RuntimeError("The dummy format cannot write image data.")
  107. def set_meta_data(self, meta):
  108. # Process the given meta data (global for all images)
  109. # It is not mandatory to support this.
  110. raise RuntimeError("The dummy format cannot write meta data.")
  111. # Register. You register an *instance* of a Format class. Here specify:
  112. format = DummyFormat(
  113. "dummy", # short name
  114. "An example format that does nothing.", # one line descr.
  115. ".foobar .nonexistentext", # list of extensions
  116. "iI", # modes, characters in iIvV
  117. )
  118. formats.add_format(format)