v3.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import numpy as np
  2. from .core.imopen import imopen
  3. def imread(uri, *, index=None, plugin=None, extension=None, format_hint=None, **kwargs):
  4. """Read an ndimage from a URI.
  5. Opens the given URI and reads an ndimage from it. The exact behavior
  6. depends on both the file type and plugin used to open the file. To learn
  7. about the exact behavior, check the documentation of the relevant plugin.
  8. Typically, imread attempts to read all data stored in the URI.
  9. Parameters
  10. ----------
  11. uri : {str, pathlib.Path, bytes, file}
  12. The resource to load the image from, e.g. a filename, pathlib.Path,
  13. http address or file object, see the docs for more info.
  14. index : {int, Ellipsis, None}
  15. If the ImageResource contains multiple ndimages, and index is an
  16. integer, select the index-th ndimage from among them and return it. If
  17. index is an ellipsis (...), read all ndimages in the file and stack them
  18. along a new batch dimension. If index is None, let the plugin decide.
  19. plugin : {str, None}
  20. The plugin to use. If set to None (default) imread will perform a
  21. search for a matching plugin. If not None, this takes priority over
  22. the provided format hint (if present).
  23. extension : str
  24. If not None, treat the provided ImageResource as if it had the given
  25. extension. This affects the order in which backends are considered.
  26. format_hint : str
  27. Deprecated. Use `extension` instead.
  28. **kwargs :
  29. Additional keyword arguments will be passed to the plugin's read call.
  30. Returns
  31. -------
  32. image : ndimage
  33. The ndimage located at the given URI.
  34. """
  35. plugin_kwargs = {
  36. "legacy_mode": False,
  37. "plugin": plugin,
  38. "format_hint": format_hint,
  39. "extension": extension,
  40. }
  41. call_kwargs = kwargs
  42. if index is not None:
  43. call_kwargs["index"] = index
  44. with imopen(uri, "r", **plugin_kwargs) as img_file:
  45. return np.asarray(img_file.read(**call_kwargs))
  46. def imiter(uri, *, plugin=None, extension=None, format_hint=None, **kwargs):
  47. """Read a sequence of ndimages from a URI.
  48. Returns an iterable that yields ndimages from the given URI. The exact
  49. behavior depends on both, the file type and plugin used to open the file.
  50. To learn about the exact behavior, check the documentation of the relevant
  51. plugin.
  52. Parameters
  53. ----------
  54. uri : {str, pathlib.Path, bytes, file}
  55. The resource to load the image from, e.g. a filename, pathlib.Path,
  56. http address or file object, see the docs for more info.
  57. plugin : {str, None}
  58. The plugin to use. If set to None (default) imiter will perform a
  59. search for a matching plugin. If not None, this takes priority over
  60. the provided format hint (if present).
  61. extension : str
  62. If not None, treat the provided ImageResource as if it had the given
  63. extension. This affects the order in which backends are considered.
  64. format_hint : str
  65. Deprecated. Use `extension` instead.
  66. **kwargs :
  67. Additional keyword arguments will be passed to the plugin's ``iter``
  68. call.
  69. Yields
  70. ------
  71. image : ndimage
  72. The next ndimage located at the given URI.
  73. """
  74. with imopen(
  75. uri,
  76. "r",
  77. legacy_mode=False,
  78. plugin=plugin,
  79. format_hint=format_hint,
  80. extension=extension,
  81. ) as img_file:
  82. for image in img_file.iter(**kwargs):
  83. # Note: casting to ndarray here to ensure compatibility
  84. # with the v2.9 API
  85. yield np.asarray(image)
  86. def imwrite(uri, image, *, plugin=None, extension=None, format_hint=None, **kwargs):
  87. """Write an ndimage to the given URI.
  88. The exact behavior depends on the file type and plugin used. To learn about
  89. the exact behavior, check the documentation of the relevant plugin.
  90. Parameters
  91. ----------
  92. uri : {str, pathlib.Path, bytes, file}
  93. The resource to save the image to, e.g. a filename, pathlib.Path,
  94. http address or file object, check the docs for more info.
  95. image : np.ndarray
  96. The image to write to disk.
  97. plugin : {str, None}
  98. The plugin to use. If set to None (default) imwrite will perform a
  99. search for a matching plugin. If not None, this takes priority over
  100. the provided format hint (if present).
  101. extension : str
  102. If not None, treat the provided ImageResource as if it had the given
  103. extension. This affects the order in which backends are considered, and
  104. may also influence the format used when encoding.
  105. format_hint : str
  106. Deprecated. Use `extension` instead.
  107. **kwargs :
  108. Additional keyword arguments will be passed to the plugin's ``write``
  109. call.
  110. Returns
  111. -------
  112. encoded_image : None or Bytes
  113. Returns ``None`` in all cases, except when ``uri`` is set to ``<bytes>``.
  114. In this case it returns the encoded ndimage as a bytes string.
  115. """
  116. with imopen(
  117. uri,
  118. "w",
  119. legacy_mode=False,
  120. plugin=plugin,
  121. format_hint=format_hint,
  122. extension=extension,
  123. ) as img_file:
  124. encoded = img_file.write(image, **kwargs)
  125. return encoded
  126. def improps(uri, *, index=None, plugin=None, extension=None, **kwargs):
  127. """Read standardized metadata.
  128. Opens the given URI and reads the properties of an ndimage from it. The
  129. properties represent standardized metadata. This means that they will have
  130. the same name regardless of the format being read or plugin/backend being
  131. used. Further, any field will be, where possible, populated with a sensible
  132. default (may be `None`) if the ImageResource does not declare a value in its
  133. metadata.
  134. Parameters
  135. ----------
  136. index : int
  137. If the ImageResource contains multiple ndimages, and index is an
  138. integer, select the index-th ndimage from among them and return its
  139. properties. If index is an ellipsis (...), read all ndimages in the file
  140. and stack them along a new batch dimension and return their properties.
  141. If index is None, let the plugin decide.
  142. plugin : {str, None}
  143. The plugin to be used. If None, performs a search for a matching
  144. plugin.
  145. extension : str
  146. If not None, treat the provided ImageResource as if it had the given
  147. extension. This affects the order in which backends are considered.
  148. **kwargs :
  149. Additional keyword arguments will be passed to the plugin's ``properties``
  150. call.
  151. Returns
  152. -------
  153. properties : ImageProperties
  154. A dataclass filled with standardized image metadata.
  155. Notes
  156. -----
  157. Where possible, this will avoid loading pixel data.
  158. See Also
  159. --------
  160. imageio.core.v3_plugin_api.ImageProperties
  161. """
  162. plugin_kwargs = {"legacy_mode": False, "plugin": plugin, "extension": extension}
  163. call_kwargs = kwargs
  164. if index is not None:
  165. call_kwargs["index"] = index
  166. with imopen(uri, "r", **plugin_kwargs) as img_file:
  167. properties = img_file.properties(**call_kwargs)
  168. return properties
  169. def immeta(
  170. uri, *, index=None, plugin=None, extension=None, exclude_applied=True, **kwargs
  171. ):
  172. """Read format-specific metadata.
  173. Opens the given URI and reads metadata for an ndimage from it. The contents
  174. of the returned metadata dictionary is specific to both the image format and
  175. plugin used to open the ImageResource. To learn about the exact behavior,
  176. check the documentation of the relevant plugin. Typically, immeta returns a
  177. dictionary specific to the image format, where keys match metadata field
  178. names and values are a field's contents.
  179. Parameters
  180. ----------
  181. uri : {str, pathlib.Path, bytes, file}
  182. The resource to load the image from, e.g. a filename, pathlib.Path, http
  183. address or file object, see the docs for more info.
  184. index : {int, None}
  185. If the ImageResource contains multiple ndimages, and index is an
  186. integer, select the index-th ndimage from among them and return its
  187. metadata. If index is an ellipsis (...), return global metadata. If
  188. index is None, let the plugin decide the default.
  189. plugin : {str, None}
  190. The plugin to be used. If None (default), performs a search for a
  191. matching plugin.
  192. extension : str
  193. If not None, treat the provided ImageResource as if it had the given
  194. extension. This affects the order in which backends are considered.
  195. **kwargs :
  196. Additional keyword arguments will be passed to the plugin's metadata
  197. method.
  198. Returns
  199. -------
  200. image : ndimage
  201. The ndimage located at the given URI.
  202. """
  203. plugin_kwargs = {"legacy_mode": False, "plugin": plugin, "extension": extension}
  204. call_kwargs = kwargs
  205. call_kwargs["exclude_applied"] = exclude_applied
  206. if index is not None:
  207. call_kwargs["index"] = index
  208. with imopen(uri, "r", **plugin_kwargs) as img_file:
  209. metadata = img_file.metadata(**call_kwargs)
  210. return metadata
  211. __all__ = ["imopen", "imread", "imwrite", "imiter", "improps", "immeta"]