v2.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. # -*- coding: utf-8 -*-
  2. # imageio is distributed under the terms of the (new) BSD License.
  3. import re
  4. import warnings
  5. from numbers import Number
  6. from pathlib import Path
  7. from typing import Dict
  8. import numpy as np
  9. from imageio.core.legacy_plugin_wrapper import LegacyPlugin
  10. from imageio.core.util import Array
  11. from imageio.core.v3_plugin_api import PluginV3
  12. from . import formats
  13. from .config import known_extensions, known_plugins
  14. from .core import RETURN_BYTES
  15. from .core.imopen import imopen
  16. MEMTEST_DEFAULT_MIM = "256MB"
  17. MEMTEST_DEFAULT_MVOL = "1GB"
  18. mem_re = re.compile(r"^(\d+\.?\d*)\s*([kKMGTPEZY]?i?)B?$")
  19. sizes = {"": 1, None: 1}
  20. for i, si in enumerate([""] + list("kMGTPEZY")):
  21. sizes[si] = 1000**i
  22. if si:
  23. sizes[si.upper() + "i"] = 1024**i
  24. def to_nbytes(arg, default=None):
  25. if not arg:
  26. arg = float("inf")
  27. if arg is True:
  28. arg = default
  29. if isinstance(arg, Number):
  30. return arg
  31. match = mem_re.match(arg)
  32. if match is None:
  33. raise ValueError(
  34. "Memory size could not be parsed "
  35. "(is your capitalisation correct?): {}".format(arg)
  36. )
  37. num, unit = match.groups()
  38. try:
  39. return float(num) * sizes[unit]
  40. except KeyError: # pragma: no cover
  41. # Note: I don't think we can reach this
  42. raise ValueError(
  43. "Memory size unit not recognised "
  44. "(is your capitalisation correct?): {}".format(unit)
  45. )
  46. def help(name=None):
  47. """help(name=None)
  48. Print the documentation of the format specified by name, or a list
  49. of supported formats if name is omitted.
  50. Parameters
  51. ----------
  52. name : str
  53. Can be the name of a format, a filename extension, or a full
  54. filename. See also the :doc:`formats page <../formats/index>`.
  55. """
  56. if not name:
  57. print(formats)
  58. else:
  59. print(formats[name])
  60. def decypher_format_arg(format_name: str) -> Dict[str, str]:
  61. """Split format into plugin and format
  62. The V2 API aliases plugins and supported formats. This function
  63. splits these so that they can be fed separately to `iio.imopen`.
  64. """
  65. plugin = None
  66. extension = None
  67. if format_name is None:
  68. pass # nothing to do
  69. elif Path(format_name).suffix.lower() in known_extensions:
  70. extension = Path(format_name).suffix.lower()
  71. elif format_name in known_plugins:
  72. plugin = format_name
  73. elif format_name.upper() in known_plugins:
  74. plugin = format_name.upper()
  75. elif format_name.lower() in known_extensions:
  76. extension = format_name.lower()
  77. elif "." + format_name.lower() in known_extensions:
  78. extension = "." + format_name.lower()
  79. else:
  80. raise IndexError(f"No format known by name `{plugin}`.")
  81. return {"plugin": plugin, "extension": extension}
  82. class LegacyReader:
  83. def __init__(self, plugin_instance: PluginV3, **kwargs):
  84. self.instance = plugin_instance
  85. self.last_index = 0
  86. self.closed = False
  87. if (
  88. type(self.instance).__name__ == "PillowPlugin"
  89. and kwargs.get("pilmode") is not None
  90. ):
  91. kwargs["mode"] = kwargs["pilmode"]
  92. del kwargs["pilmode"]
  93. self.read_args = kwargs
  94. def close(self):
  95. if not self.closed:
  96. self.instance.close()
  97. self.closed = True
  98. def __enter__(self):
  99. return self
  100. def __exit__(self, type, value, traceback):
  101. self.close()
  102. def __del__(self):
  103. self.close()
  104. @property
  105. def request(self):
  106. return self.instance.request
  107. @property
  108. def format(self):
  109. raise TypeError("V3 Plugins don't have a format.")
  110. def get_length(self):
  111. return self.instance.properties(index=...).n_images
  112. def get_data(self, index):
  113. self.last_index = index
  114. img = self.instance.read(index=index, **self.read_args)
  115. metadata = self.instance.metadata(index=index, exclude_applied=False)
  116. return Array(img, metadata)
  117. def get_next_data(self):
  118. return self.get_data(self.last_index + 1)
  119. def set_image_index(self, index):
  120. self.last_index = index - 1
  121. def get_meta_data(self, index=None):
  122. return self.instance.metadata(index=index, exclude_applied=False)
  123. def iter_data(self):
  124. for idx, img in enumerate(self.instance.iter()):
  125. metadata = self.instance.metadata(index=idx, exclude_applied=False)
  126. yield Array(img, metadata)
  127. def __iter__(self):
  128. return self.iter_data()
  129. def __len__(self):
  130. return self.get_length()
  131. class LegacyWriter:
  132. def __init__(self, plugin_instance: PluginV3, **kwargs):
  133. self.instance = plugin_instance
  134. self.last_index = 0
  135. self.closed = False
  136. if type(self.instance).__name__ == "PillowPlugin" and "pilmode" in kwargs:
  137. kwargs["mode"] = kwargs["pilmode"]
  138. del kwargs["pilmode"]
  139. self.write_args = kwargs
  140. def close(self):
  141. if not self.closed:
  142. self.instance.close()
  143. self.closed = True
  144. def __enter__(self):
  145. return self
  146. def __exit__(self, type, value, traceback):
  147. self.close()
  148. def __del__(self):
  149. self.close()
  150. @property
  151. def request(self):
  152. return self.instance.request
  153. @property
  154. def format(self):
  155. raise TypeError("V3 Plugins don't have a format.")
  156. def append_data(self, im, meta=None):
  157. # TODO: write metadata in the future; there is currently no
  158. # generic way to do this with v3 plugins :(
  159. if meta is not None:
  160. warnings.warn(
  161. "V3 Plugins currently don't have a uniform way to"
  162. " write metadata, so any metadata is ignored."
  163. )
  164. # total_meta = dict()
  165. # if meta is None:
  166. # meta = {}
  167. # if hasattr(im, "meta") and isinstance(im.meta, dict):
  168. # total_meta.update(im.meta)
  169. # total_meta.update(meta)
  170. return self.instance.write(im, **self.write_args)
  171. def set_meta_data(self, meta):
  172. # TODO: write metadata
  173. raise NotImplementedError(
  174. "V3 Plugins don't have a uniform way to write metadata (yet)."
  175. )
  176. def is_batch(ndimage):
  177. if isinstance(ndimage, (list, tuple)):
  178. return True
  179. ndimage = np.asarray(ndimage)
  180. if ndimage.ndim <= 2:
  181. return False
  182. elif ndimage.ndim == 3 and ndimage.shape[2] < 5:
  183. return False
  184. return True
  185. def is_volume(ndimage):
  186. ndimage = np.asarray(ndimage)
  187. if not is_batch(ndimage):
  188. return False
  189. if ndimage.ndim == 3 and ndimage.shape[2] >= 5:
  190. return True
  191. elif ndimage.ndim == 4 and ndimage.shape[3] < 5:
  192. return True
  193. else:
  194. return False
  195. # Base functions that return a reader/writer
  196. def get_reader(uri, format=None, mode="?", **kwargs):
  197. """get_reader(uri, format=None, mode='?', **kwargs)
  198. Returns a :class:`.Reader` object which can be used to read data
  199. and meta data from the specified file.
  200. Parameters
  201. ----------
  202. uri : {str, pathlib.Path, bytes, file}
  203. The resource to load the image from, e.g. a filename, pathlib.Path,
  204. http address or file object, see the docs for more info.
  205. format : str
  206. The format to use to read the file. By default imageio selects
  207. the appropriate for you based on the filename and its contents.
  208. mode : {'i', 'I', 'v', 'V', '?'}
  209. Used to give the reader a hint on what the user expects (default "?"):
  210. "i" for an image, "I" for multiple images, "v" for a volume,
  211. "V" for multiple volumes, "?" for don't care.
  212. kwargs : ...
  213. Further keyword arguments are passed to the reader. See :func:`.help`
  214. to see what arguments are available for a particular format.
  215. """
  216. imopen_args = decypher_format_arg(format)
  217. imopen_args["legacy_mode"] = True
  218. image_file = imopen(uri, "r" + mode, **imopen_args)
  219. if isinstance(image_file, LegacyPlugin):
  220. return image_file.legacy_get_reader(**kwargs)
  221. else:
  222. return LegacyReader(image_file, **kwargs)
  223. def get_writer(uri, format=None, mode="?", **kwargs):
  224. """get_writer(uri, format=None, mode='?', **kwargs)
  225. Returns a :class:`.Writer` object which can be used to write data
  226. and meta data to the specified file.
  227. Parameters
  228. ----------
  229. uri : {str, pathlib.Path, file}
  230. The resource to write the image to, e.g. a filename, pathlib.Path
  231. or file object, see the docs for more info.
  232. format : str
  233. The format to use to write the file. By default imageio selects
  234. the appropriate for you based on the filename.
  235. mode : {'i', 'I', 'v', 'V', '?'}
  236. Used to give the writer a hint on what the user expects (default '?'):
  237. "i" for an image, "I" for multiple images, "v" for a volume,
  238. "V" for multiple volumes, "?" for don't care.
  239. kwargs : ...
  240. Further keyword arguments are passed to the writer. See :func:`.help`
  241. to see what arguments are available for a particular format.
  242. """
  243. imopen_args = decypher_format_arg(format)
  244. imopen_args["legacy_mode"] = True
  245. image_file = imopen(uri, "w" + mode, **imopen_args)
  246. if isinstance(image_file, LegacyPlugin):
  247. return image_file.legacy_get_writer(**kwargs)
  248. else:
  249. return LegacyWriter(image_file, **kwargs)
  250. # Images
  251. def imread(uri, format=None, **kwargs):
  252. """imread(uri, format=None, **kwargs)
  253. Reads an image from the specified file. Returns a numpy array, which
  254. comes with a dict of meta data at its 'meta' attribute.
  255. Note that the image data is returned as-is, and may not always have
  256. a dtype of uint8 (and thus may differ from what e.g. PIL returns).
  257. Parameters
  258. ----------
  259. uri : {str, pathlib.Path, bytes, file}
  260. The resource to load the image from, e.g. a filename, pathlib.Path,
  261. http address or file object, see the docs for more info.
  262. format : str
  263. The format to use to read the file. By default imageio selects
  264. the appropriate for you based on the filename and its contents.
  265. kwargs : ...
  266. Further keyword arguments are passed to the reader. See :func:`.help`
  267. to see what arguments are available for a particular format.
  268. """
  269. imopen_args = decypher_format_arg(format)
  270. imopen_args["legacy_mode"] = True
  271. with imopen(uri, "ri", **imopen_args) as file:
  272. result = file.read(index=0, **kwargs)
  273. return result
  274. def imwrite(uri, im, format=None, **kwargs):
  275. """imwrite(uri, im, format=None, **kwargs)
  276. Write an image to the specified file.
  277. Parameters
  278. ----------
  279. uri : {str, pathlib.Path, file}
  280. The resource to write the image to, e.g. a filename, pathlib.Path
  281. or file object, see the docs for more info.
  282. im : numpy.ndarray
  283. The image data. Must be NxM, NxMx3 or NxMx4.
  284. format : str
  285. The format to use to write the file. By default imageio selects
  286. the appropriate for you based on the filename and its contents.
  287. kwargs : ...
  288. Further keyword arguments are passed to the writer. See :func:`.help`
  289. to see what arguments are available for a particular format.
  290. """
  291. # Test image
  292. imt = type(im)
  293. im = np.asarray(im)
  294. if not np.issubdtype(im.dtype, np.number):
  295. raise ValueError("Image is not numeric, but {}.".format(imt.__name__))
  296. if is_batch(im) or im.ndim < 2:
  297. raise ValueError("Image must be 2D (grayscale, RGB, or RGBA).")
  298. imopen_args = decypher_format_arg(format)
  299. imopen_args["legacy_mode"] = True
  300. with imopen(uri, "wi", **imopen_args) as file:
  301. return file.write(im, **kwargs)
  302. # Multiple images
  303. def mimread(uri, format=None, memtest=MEMTEST_DEFAULT_MIM, **kwargs):
  304. """mimread(uri, format=None, memtest="256MB", **kwargs)
  305. Reads multiple images from the specified file. Returns a list of
  306. numpy arrays, each with a dict of meta data at its 'meta' attribute.
  307. Parameters
  308. ----------
  309. uri : {str, pathlib.Path, bytes, file}
  310. The resource to load the images from, e.g. a filename,pathlib.Path,
  311. http address or file object, see the docs for more info.
  312. format : str
  313. The format to use to read the file. By default imageio selects
  314. the appropriate for you based on the filename and its contents.
  315. memtest : {bool, int, float, str}
  316. If truthy, this function will raise an error if the resulting
  317. list of images consumes greater than the amount of memory specified.
  318. This is to protect the system from using so much memory that it needs
  319. to resort to swapping, and thereby stall the computer. E.g.
  320. ``mimread('hunger_games.avi')``.
  321. If the argument is a number, that will be used as the threshold number
  322. of bytes.
  323. If the argument is a string, it will be interpreted as a number of bytes with
  324. SI/IEC prefixed units (e.g. '1kB', '250MiB', '80.3YB').
  325. - Units are case sensitive
  326. - k, M etc. represent a 1000-fold change, where Ki, Mi etc. represent 1024-fold
  327. - The "B" is optional, but if present, must be capitalised
  328. If the argument is True, the default will be used, for compatibility reasons.
  329. Default: '256MB'
  330. kwargs : ...
  331. Further keyword arguments are passed to the reader. See :func:`.help`
  332. to see what arguments are available for a particular format.
  333. """
  334. # used for mimread and mvolread
  335. nbyte_limit = to_nbytes(memtest, MEMTEST_DEFAULT_MIM)
  336. images = list()
  337. nbytes = 0
  338. imopen_args = decypher_format_arg(format)
  339. imopen_args["legacy_mode"] = True
  340. with imopen(uri, "rI", **imopen_args) as file:
  341. for image in file.iter(**kwargs):
  342. images.append(image)
  343. nbytes += image.nbytes
  344. if nbytes > nbyte_limit:
  345. raise RuntimeError(
  346. "imageio.mimread() has read over {}B of "
  347. "image data.\nStopped to avoid memory problems."
  348. " Use imageio.get_reader(), increase threshold, or memtest=False".format(
  349. int(nbyte_limit)
  350. )
  351. )
  352. if len(images) == 1 and is_batch(images[0]):
  353. images = [*images[0]]
  354. return images
  355. def mimwrite(uri, ims, format=None, **kwargs):
  356. """mimwrite(uri, ims, format=None, **kwargs)
  357. Write multiple images to the specified file.
  358. Parameters
  359. ----------
  360. uri : {str, pathlib.Path, file}
  361. The resource to write the images to, e.g. a filename, pathlib.Path
  362. or file object, see the docs for more info.
  363. ims : sequence of numpy arrays
  364. The image data. Each array must be NxM, NxMx3 or NxMx4.
  365. format : str
  366. The format to use to read the file. By default imageio selects
  367. the appropriate for you based on the filename and its contents.
  368. kwargs : ...
  369. Further keyword arguments are passed to the writer. See :func:`.help`
  370. to see what arguments are available for a particular format.
  371. """
  372. if not is_batch(ims):
  373. raise ValueError("Image data must be a sequence of ndimages.")
  374. imopen_args = decypher_format_arg(format)
  375. imopen_args["legacy_mode"] = True
  376. with imopen(uri, "wI", **imopen_args) as file:
  377. return file.write(ims, is_batch=True, **kwargs)
  378. # Volumes
  379. def volread(uri, format=None, **kwargs):
  380. """volread(uri, format=None, **kwargs)
  381. Reads a volume from the specified file. Returns a numpy array, which
  382. comes with a dict of meta data at its 'meta' attribute.
  383. Parameters
  384. ----------
  385. uri : {str, pathlib.Path, bytes, file}
  386. The resource to load the volume from, e.g. a filename, pathlib.Path,
  387. http address or file object, see the docs for more info.
  388. format : str
  389. The format to use to read the file. By default imageio selects
  390. the appropriate for you based on the filename and its contents.
  391. kwargs : ...
  392. Further keyword arguments are passed to the reader. See :func:`.help`
  393. to see what arguments are available for a particular format.
  394. """
  395. imopen_args = decypher_format_arg(format)
  396. imopen_args["legacy_mode"] = True
  397. with imopen(uri, "rv", **imopen_args) as file:
  398. return file.read(index=0, **kwargs)
  399. def volwrite(uri, im, format=None, **kwargs):
  400. """volwrite(uri, vol, format=None, **kwargs)
  401. Write a volume to the specified file.
  402. Parameters
  403. ----------
  404. uri : {str, pathlib.Path, file}
  405. The resource to write the image to, e.g. a filename, pathlib.Path
  406. or file object, see the docs for more info.
  407. vol : numpy.ndarray
  408. The image data. Must be NxMxL (or NxMxLxK if each voxel is a tuple).
  409. format : str
  410. The format to use to read the file. By default imageio selects
  411. the appropriate for you based on the filename and its contents.
  412. kwargs : ...
  413. Further keyword arguments are passed to the writer. See :func:`.help`
  414. to see what arguments are available for a particular format.
  415. """
  416. # Test image
  417. im = np.asarray(im)
  418. if not is_volume(im):
  419. raise ValueError("Image must be 3D, or 4D if each voxel is a tuple.")
  420. imopen_args = decypher_format_arg(format)
  421. imopen_args["legacy_mode"] = True
  422. with imopen(uri, "wv", **imopen_args) as file:
  423. return file.write(im, is_batch=False, **kwargs)
  424. # Multiple volumes
  425. def mvolread(uri, format=None, memtest=MEMTEST_DEFAULT_MVOL, **kwargs):
  426. """mvolread(uri, format=None, memtest='1GB', **kwargs)
  427. Reads multiple volumes from the specified file. Returns a list of
  428. numpy arrays, each with a dict of meta data at its 'meta' attribute.
  429. Parameters
  430. ----------
  431. uri : {str, pathlib.Path, bytes, file}
  432. The resource to load the volumes from, e.g. a filename, pathlib.Path,
  433. http address or file object, see the docs for more info.
  434. format : str
  435. The format to use to read the file. By default imageio selects
  436. the appropriate for you based on the filename and its contents.
  437. memtest : {bool, int, float, str}
  438. If truthy, this function will raise an error if the resulting
  439. list of images consumes greater than the amount of memory specified.
  440. This is to protect the system from using so much memory that it needs
  441. to resort to swapping, and thereby stall the computer. E.g.
  442. ``mimread('hunger_games.avi')``.
  443. If the argument is a number, that will be used as the threshold number
  444. of bytes.
  445. If the argument is a string, it will be interpreted as a number of bytes with
  446. SI/IEC prefixed units (e.g. '1kB', '250MiB', '80.3YB').
  447. - Units are case sensitive
  448. - k, M etc. represent a 1000-fold change, where Ki, Mi etc. represent 1024-fold
  449. - The "B" is optional, but if present, must be capitalised
  450. If the argument is True, the default will be used, for compatibility reasons.
  451. Default: '1GB'
  452. kwargs : ...
  453. Further keyword arguments are passed to the reader. See :func:`.help`
  454. to see what arguments are available for a particular format.
  455. """
  456. # used for mimread and mvolread
  457. nbyte_limit = to_nbytes(memtest, MEMTEST_DEFAULT_MVOL)
  458. images = list()
  459. nbytes = 0
  460. imopen_args = decypher_format_arg(format)
  461. imopen_args["legacy_mode"] = True
  462. with imopen(uri, "rV", **imopen_args) as file:
  463. for image in file.iter(**kwargs):
  464. images.append(image)
  465. nbytes += image.nbytes
  466. if nbytes > nbyte_limit:
  467. raise RuntimeError(
  468. "imageio.mimread() has read over {}B of "
  469. "image data.\nStopped to avoid memory problems."
  470. " Use imageio.get_reader(), increase threshold, or memtest=False".format(
  471. int(nbyte_limit)
  472. )
  473. )
  474. return images
  475. def mvolwrite(uri, ims, format=None, **kwargs):
  476. """mvolwrite(uri, vols, format=None, **kwargs)
  477. Write multiple volumes to the specified file.
  478. Parameters
  479. ----------
  480. uri : {str, pathlib.Path, file}
  481. The resource to write the volumes to, e.g. a filename, pathlib.Path
  482. or file object, see the docs for more info.
  483. ims : sequence of numpy arrays
  484. The image data. Each array must be NxMxL (or NxMxLxK if each
  485. voxel is a tuple).
  486. format : str
  487. The format to use to read the file. By default imageio selects
  488. the appropriate for you based on the filename and its contents.
  489. kwargs : ...
  490. Further keyword arguments are passed to the writer. See :func:`.help`
  491. to see what arguments are available for a particular format.
  492. """
  493. for im in ims:
  494. if not is_volume(im):
  495. raise ValueError("Image must be 3D, or 4D if each voxel is a tuple.")
  496. imopen_args = decypher_format_arg(format)
  497. imopen_args["legacy_mode"] = True
  498. with imopen(uri, "wV", **imopen_args) as file:
  499. return file.write(ims, is_batch=True, **kwargs)
  500. # aliases
  501. read = get_reader
  502. save = get_writer
  503. imsave = imwrite
  504. mimsave = mimwrite
  505. volsave = volwrite
  506. mvolsave = mvolwrite
  507. __all__ = [
  508. "imread",
  509. "mimread",
  510. "volread",
  511. "mvolread",
  512. "imwrite",
  513. "mimwrite",
  514. "volwrite",
  515. "mvolwrite",
  516. # misc
  517. "help",
  518. "get_reader",
  519. "get_writer",
  520. "RETURN_BYTES",
  521. ]