1# -*- coding: utf-8 -*-
2# imageio is distributed under the terms of the (new) BSD License.
3
4"""Read/Write NPZ files.
5
6Backend: `Numpy <https://numpy.org/doc/stable/reference/generated/numpy.savez.html>`_
7
8NPZ is a file format by numpy that provides storage of array data using gzip
9compression. This imageio plugin supports data of any shape, and also supports
10multiple images per file. However, the npz format does not provide streaming;
11all data is read/written at once. Further, there is no support for meta data.
12
13See the BSDF format for a similar (but more fully featured) format.
14
15Parameters
16----------
17None
18
19Notes
20-----
21This format is not available on Pypy.
22
23"""
24
25import numpy as np
26
27from ..core import Format
28
29
30class NpzFormat(Format):
31 """See :mod:`imageio.plugins.npz`"""
32
33 def _can_read(self, request):
34 # We support any kind of image data
35 return request.extension in self.extensions
36
37 def _can_write(self, request):
38 # We support any kind of image data
39 return request.extension in self.extensions
40
41 # -- reader
42
43 class Reader(Format.Reader):
44 def _open(self):
45 # Load npz file, which provides another file like object
46 self._npz = np.load(self.request.get_file())
47 assert isinstance(self._npz, np.lib.npyio.NpzFile)
48 # Get list of names, ordered by name, but smarter
49 self._names = sorted(self._npz.files, key=lambda x: x.split("_")[-1])
50
51 def _close(self):
52 self._npz.close()
53
54 def _get_length(self):
55 return len(self._names)
56
57 def _get_data(self, index):
58 # Get data
59 if index < 0 or index >= len(self._names):
60 raise IndexError("Index out of range while reading from nzp")
61 im = self._npz[self._names[index]]
62 # Return array and empty meta data
63 return im, {}
64
65 def _get_meta_data(self, index):
66 # Get the meta data for the given index
67 raise RuntimeError("The npz format does not support meta data.")
68
69 # -- writer
70
71 class Writer(Format.Writer):
72 def _open(self):
73 # Npz is not such a great format. We cannot stream to the file.
74 # So we remember all images and write them to file at the end.
75 self._images = []
76
77 def _close(self):
78 # Write everything
79 np.savez_compressed(self.request.get_file(), *self._images)
80
81 def _append_data(self, im, meta):
82 self._images.append(im) # discart meta data
83
84 def set_meta_data(self, meta):
85 raise RuntimeError("The npz format does not support meta data.")