Debian Codegraph @

docs/reference/userapi.rst

raw
1 -----------
2 Core API v2
3 -----------
4
5 .. py:currentmodule:: imageio.v2
6
7 .. warning::
8
9 This API exists for backwards compatibility. It is a wrapper around calls to
10 the v3 API and new code should use the v3 API directly. Check the migration
11 instructions below for detailed information on how to migrate.
12
13 These functions represent imageio's main interface for the user. They
14 provide a common API to read and write image data for a large
15 variety of formats. All read and write functions accept keyword
16 arguments, which are passed on to the backend that does the actual work.
17 To see what keyword arguments are supported by a specific format, use
18 the :func:`.help` function.
19
20 .. note::
21 All read-functions return images as numpy arrays, and have a ``meta``
22 attribute; the meta-data dictionary can be accessed with ``im.meta``.
23 To make this work, imageio actually makes use of a subclass of
24 ``np.ndarray``. If needed, the image can be converted to a plain numpy
25 array using ``np.asarray(im)``.
26
27 .. autofunction:: imageio.help
28
29 .. autofunction :: imageio.show_formats
30
31
32 Functions for reading
33 ^^^^^^^^^^^^^^^^^^^^^
34
35 .. autosummary::
36 :toctree: ../_autosummary/
37
38 imageio.v2.imread
39 imageio.v2.mimread
40 imageio.v2.volread
41 imageio.v2.mvolread
42
43
44 Functions for writing
45 ^^^^^^^^^^^^^^^^^^^^^
46
47 .. autosummary::
48 :toctree: ../_autosummary/
49
50 imageio.v2.imwrite
51 imageio.v2.mimwrite
52 imageio.v2.volwrite
53 imageio.v2.mvolwrite
54
55 More control
56 ^^^^^^^^^^^^
57
58 For a larger degree of control, imageio provides functions
59 :func:`.get_reader` and :func:`.get_writer`. They respectively return an
60 :class:`.Reader` and an :class:`.Writer` object, which can
61 be used to read/write data and meta data in a more controlled manner.
62 This also allows specific scientific formats to be exposed in a way
63 that best suits that file-format.
64
65
66 .. autofunction:: imageio.v2.get_reader
67
68 .. autofunction:: imageio.v2.get_writer
69
70 ----
71
72 .. autoclass:: imageio.core::Format.Reader
73 :inherited-members:
74 :members:
75
76 .. autoclass:: imageio.core::Format.Writer
77 :inherited-members:
78 :members:
79
80 .. _migration_from_v2:
81
82 Migrating to the v3 API
83 ^^^^^^^^^^^^^^^^^^^^^^^
84
85 .. note::
86 Make sure to have a look at the :doc:`narrative docs for the v3 API <core_v3>`
87 to build some intuition on how to use the new API.
88
89 The primary novelty of the v3 API compared to the v2 API is that images are now
90 treated as ndimages, a generalization of (flat) 2D images to N dimensions. This
91 change allows us to design a much simpler API; however, comes with a few
92 backwards incompatible changes.
93
94 .. rubric:: Avoiding Migration for Legacy Scripts
95
96 If you have an old script that you do not wish to migrate, you can avoid most of
97 the migration by explicitly importing the v2 API::
98
99 import imageio.v2 as imageio
100
101 This will give you access to most of the old API. However, calls will still rely
102 on the new (v3) plugins and backends, which may cause behavioral changes. As
103 such, you should prefer a full migration to v3 and should avoid using the v2 API
104 in new code. This is meant as a quick way for you to postpone a full migration
105 until it is convenient for you to do so.
106
107
108 Reading Images
109 --------------
110
111 `iio.imread` can now return a ndimage instead of being limited to flat images.
112 As such, `iio.volread` has merged into `iio.imread` and is now gone. Similarly,
113 `iio.mimread` and `iio.mvolread` have merged into a new function called
114 `iio.imiter`, which returns a generator that yields ndimages from a file in the
115 order in which they appear. Further, the default behavior of `iio.imread` has
116 changed and it has become more context aware. Now it will either return the
117 first stored ndimage (like before) or, depending on the format, all images if
118 this is the more common use-case.
119
120 To reproduce similar behavior to the v2 API in cases where the incompatibility
121 matters use one of the following snippets::
122
123 # img = iio.v2.imread(image_resource)
124 img = iio.v3.imread(image_resource, index=...)[0]
125
126 # img = iio.v2.volread(image_resource)
127 img = iio.v3.imread(image_resource)
128
129 # img = iio.v2.mimread(image_resource)
130 # img = iio.v2.mvolread(image_resource)
131 img = [im for im in iio.v3.imiter(image_resource)]
132
133 Writing Images
134 --------------
135
136 Similar to reading images, the new `iio.imwrite` can handle ndimages and lists
137 of images, like ``iio.mimwrite`` and consorts. ``iio.mimwrite``,
138 ``iio.volwrite``, and ``iio.mvolwrite`` have all disappeared. The same goes for
139 their aliases ``iio.mimsave``, ``iio.volsave``, ``iio.mvolsave``, and
140 ``iio.imsave``. They are now all covered by ``iio.imwrite``.
141
142 To reproduce similar behavior to the v2 API behavior use one of the following
143 snippets::
144
145 # img = iio.v2.imwrite(image_resource, ndimage)
146 # img = iio.v2.volwrite(image_resource, ndimage)
147 img = iio.v3.imwrite(image_resource, ndimage)
148
149 # img = iio.v2.mimwrite(image_resource, list_of_ndimages)
150 # img = iio.v2.mvolwrite(image_resource, list_of_ndimages)
151 img = iio.v3.imwrite(image_resource, list_of_ndimages)
152
153 Reader and Writer
154 -----------------
155
156 Previously, the `reader` and `writer` objects provided an advanced way to
157 read/write data with more control. These are no longer needed. Likewise, the
158 functions ``iio.get_reader``, ``iio.get_writer``, ``iio.read``, and ``iio.save``
159 have become obsolete. Instead, the plugin object is used directly, which is
160 instantiated in mode ``r`` (reading) or ``w`` (writing). To create such a
161 plugin object, call ``iio.imopen`` and use it as a context manager::
162
163 # reader = iio.get_reader(image_resource)
164 # reader = iio.read(image_resource)
165 with iio.imopen(image_resource, "r") as reader:
166 # file is only open/accessible here
167 # img = reader.read(index=0)
168 # meta = reader.metadata(index=0)
169 # ...
170 pass
171
172
173 # writer = iio.get_writer(image_resource)
174 # writer = iio.save(image_resource)
175 with iio.imopen(image_resource, "w") as writer:
176 # file is only open/accessible here
177 # writer.write(ndimage)
178 # ...
179 pass
180
181 Metadata
182 --------
183
184 The v3 API makes handling of metadata much more consistent and explicit.
185 Previously in v2, metadata was provided as a ``dict`` that was attached to the
186 image by returning a custom subclass of ``np.ndarray``. Now metadata is
187 served independently from pixel data::
188
189 # metadata = iio.imread(image_resource).meta
190 metadata = iio.immeta(image_resource)
191
192 Further, ImageIO now provides a curated set of standardized metadata, which is
193 called :class:`ImageProperties <imageio.core.v3_plugin_api.ImageProperties>`, in
194 addition to above metadata. The difference between the two is as follows: The
195 metadata dict contains metadata using format-specific keys to the extent the
196 reading plugin supports them. The ImageProperties dataclass contains generally
197 available metadata using standardized attribute names. Each plugin provides the
198 same set of properties, and if the plugin or format doesn't provide a field it
199 is set to a default value; usually ``None``. To access ImageProperties use::
200
201 props = iio.improps(image_resource)
202
203
204 Caveats and Notes
205 -----------------
206
207 This section is a collection of notes and feedback that we got from other
208 developers that were migrating to ImageIO V3, which might be useful to know
209 while you are migrating your own code.
210
211 - The old ``format`` kwarg has been deprecated in favor of ``plugin`` and
212 ``extension`` respectively. Use ``plugin`` to select the plugin/backend to use
213 and ``extension`` to select the file extension (aka. format).
214 - The old pillow plugin used to ``np.squeeze`` the image before writing it. This
215 has been removed in V3 to match pillows native behavior. A trailing axis with
216 dimension 1, e.g., ``(256, 256, 1)`` will now raise an exception. (see `#842
217 <https://github.com/imageio/imageio/issues/842>`_)
218 - The old pillow plugin featured a kwarg called ``as_gray`` which would convert
219 images to grayscale before returning them. This is redundant and has been
220 deprecated in favor of using ``mode="F"``(backwards compatible) or
221 ``mode="L"`` (integer result), which is pillow's native kwarg for controlling
222 the returned color space.
223 - When reading 16-bit grayscale PNGs using pillow, the v2 pillow plugin used a
224 hardcoded copy to convert pillow's 32-bit result to 16-bit. We removed this
225 copy in v3, which means that you get a 32-bit result when using an older
226 version of pillow (pre v10). Starting with pillow v10, pillow can directly
227 decode into a 16-bit array resulting in a 16-bit result from ImageIO.
228