| 1 |
----------- |
| 2 |
Core API v3 |
| 3 |
----------- |
| 4 |
|
| 5 |
.. py:currentmodule:: imageio.v3 |
| 6 |
|
| 7 |
.. note:: |
| 8 |
For migration instructions from the v2 API check :ref:`our migration guide |
| 9 |
<migration_from_v2>`. |
| 10 |
|
| 11 |
This API provides a functional interface to all of ImageIOs backends. It is |
| 12 |
split into two parts: :ref:`Everyday Usage <v3_basic_usage>` and :ref:`Advanced |
| 13 |
Usage <v3_advanced_usage>`. The first part (everyday usage) focusses on sensible |
| 14 |
defaults and automation to cover everyday tasks such as plain reading or writing |
| 15 |
of images. The second part (advanced usage) focusses on providing fully-featured |
| 16 |
and performant access to all the features of a particular backend. |
| 17 |
|
| 18 |
API Overview |
| 19 |
------------ |
| 20 |
|
| 21 |
.. autosummary:: |
| 22 |
:toctree: ../_autosummary/ |
| 23 |
|
| 24 |
imageio.v3.imread |
| 25 |
imageio.v3.imwrite |
| 26 |
imageio.v3.imiter |
| 27 |
imageio.v3.improps |
| 28 |
imageio.v3.immeta |
| 29 |
imageio.v3.imopen |
| 30 |
|
| 31 |
.. _v3_basic_usage: |
| 32 |
|
| 33 |
Everyday Usage |
| 34 |
-------------- |
| 35 |
|
| 36 |
Reading and Writing |
| 37 |
^^^^^^^^^^^^^^^^^^^ |
| 38 |
|
| 39 |
Frequently, the image-related IO you wish to perform can be summarized as "I |
| 40 |
want to load X as a numpy array.", or "I want to save my array as a XYZ file". |
| 41 |
This should not be complicated, and it is, in fact, as simple as calling the |
| 42 |
corresponding function:: |
| 43 |
|
| 44 |
import imageio.v3 as iio |
| 45 |
|
| 46 |
# reading: |
| 47 |
img = iio.imread("imageio:chelsea.png") |
| 48 |
|
| 49 |
# writing: |
| 50 |
iio.imwrite("out_image.jpg", img) |
| 51 |
|
| 52 |
# (Check the examples section for many more examples.) |
| 53 |
|
| 54 |
Both functions accept any :doc:`ImageResource <../user_guide/requests>` as |
| 55 |
their first argument, which means you can read/write almost anything image |
| 56 |
related; from simple JPGs through ImageJ hyperstacks to MP4 video or RAW formats. |
| 57 |
Check the :doc:`Supported Formats <../formats/index>` docs for a list of all formats |
| 58 |
(that we are aware of). |
| 59 |
|
| 60 |
If you do need to customize or tweak this process you can pass additional keyword |
| 61 |
arguments (`kwargs`) to overwrite any defaults ImageIO uses when reading or |
| 62 |
writing. The following `kwargs` are always available: |
| 63 |
|
| 64 |
- ``plugin``: The name of the plugin to use for reading/writing. |
| 65 |
- ``extension``: Treat the provided ImageResource as if it had the given |
| 66 |
extension. |
| 67 |
- ``index`` (reading only): The index of the ndimage to read. Useful if the format |
| 68 |
supports storing more than one (GIF, multi-page TIFF, video, etc.). |
| 69 |
|
| 70 |
Additional `kwargs` are specific to the plugin being used and you can find a |
| 71 |
list of available ones in the documentation of the specific plugin (where they |
| 72 |
are listed as parameters). A list of all available plugins can be found :ref:`here |
| 73 |
<supported_backends>` and you can read more about the above kwargs in |
| 74 |
the documentation of the respective function (imread/imwrite/etc.). |
| 75 |
|
| 76 |
Handling Metadata |
| 77 |
^^^^^^^^^^^^^^^^^ |
| 78 |
|
| 79 |
ImageIO serves two types of metadata: ImageProperties and format-specific metadata. |
| 80 |
|
| 81 |
:class:`ImageProperties <imageio.core.v3_plugin_api.ImageProperties>` are a |
| 82 |
collection of standardized metadata fields and are supported by all plugins and |
| 83 |
for all supported formats. If a file doesn't carry the relevant field or if a |
| 84 |
format doesn't support it, its value is set to a sensible default. You can |
| 85 |
access the properties of an image by calling :func:`improps |
| 86 |
<imageio.v3.improps>`:: |
| 87 |
|
| 88 |
import imageio.v3 as iio |
| 89 |
|
| 90 |
props = iio.improps("imageio:newtonscradle.gif") |
| 91 |
props.shape |
| 92 |
props.dtype |
| 93 |
# ... |
| 94 |
|
| 95 |
As with the other functions of the API, you can pass generally available kwargs |
| 96 |
(`plugin`, `index`, ...) to `improps` to modify the behavior. Plugins may |
| 97 |
specify additional plugin-specific keyword arguments, and those are documented |
| 98 |
in the plugin-specific docs. Further, accessing this metadata is efficient in |
| 99 |
the sense that it doesn't load (decode) pixel data. |
| 100 |
|
| 101 |
Format-specific metadata is handled by :func:`immeta <imageio.v3.immeta>`:: |
| 102 |
|
| 103 |
import imageio.v3 as iio |
| 104 |
|
| 105 |
meta = iio.immeta("imageio:newtonscradle.gif") |
| 106 |
|
| 107 |
It returns a dictionary of non-standardized metadata fields. It, too, supports |
| 108 |
general kwargs (`plugin`, `index`, ...) which may be extended by a specific |
| 109 |
plugin. Further, it accepts a kwarg called ``exclude_applied``. If set to True, |
| 110 |
this will remove any items from the dictionary that would be consumed by a read |
| 111 |
call to the plugin. For example, if the metadata sets a rotation flag (the raw |
| 112 |
pixel data should be rotated before displaying it) and the plugin's read call |
| 113 |
will rotate the image because if it, then setting ``exclude_applied=True`` will |
| 114 |
remove the rotation field from the returned metadata. This can be useful to keep |
| 115 |
an image and it's metadata in sync. |
| 116 |
|
| 117 |
Further, this type of metadata is much more specific than ImageProperties |
| 118 |
because different plugins (and formats) may return and support different fields. |
| 119 |
This means that you can get much more specific metadata for a format, e.g., a |
| 120 |
frame's side data in a video format, but you need to be mindful of the plugin |
| 121 |
and format used because these fields may not exist all the time, e.g., jpeg and |
| 122 |
most other image formats have no side data. In general we try to match a fields |
| 123 |
name to the name it has in a format; however, we may adjust this if the name is |
| 124 |
not a valid python string. The best way to know which fields exist for your |
| 125 |
specific ImageResource is to call ``immeta`` on it and inspect the result. |
| 126 |
|
| 127 |
.. _v3_advanced_usage: |
| 128 |
|
| 129 |
Advanced Usage |
| 130 |
-------------- |
| 131 |
|
| 132 |
Iterating Video and Multi-Page Formats |
| 133 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 134 |
|
| 135 |
Some formats (GIF, MP4, TIFF, among others) allow storage of more than one |
| 136 |
ndimage in the same file, and you may wish to process all ndimages in such a |
| 137 |
file instead of just a single one. While :func:`iio.imread(...) |
| 138 |
<imageio.core.v3_api.imread>` allows you to read all ndimages and stack them |
| 139 |
into a ndarray (using `index=...`; check the docs), this comes with two |
| 140 |
problems: |
| 141 |
|
| 142 |
- Some formats (e.g., SPE or TIFF) allow multiple ndimages with different shapes |
| 143 |
in the same file, which prevents stacking. |
| 144 |
- Some files (especially video) are too big to fit into memory when loaded all |
| 145 |
at once. |
| 146 |
|
| 147 |
To address these problems, the v3 API introduces :func:`iio.imiter |
| 148 |
<imageio.core.v3_api.imiter>`; a generator yielding ndimages in the order in |
| 149 |
which they appear in the file:: |
| 150 |
|
| 151 |
import imageio.v3 as iio |
| 152 |
|
| 153 |
for frame in iio.imiter("imageio:cockatoo.mp4"): |
| 154 |
pass # do something with each frame |
| 155 |
|
| 156 |
Just like imread, imiter accepts additional `kwargs` to overwrite any defaults |
| 157 |
used by ImageIO. Like before, the :func:`function-specific documentation |
| 158 |
<imageio.core.v3_api.imiter>` details the `kwargs` that are always present, and |
| 159 |
additional kwargs are plugin specific and documented by the respective plugin. |
| 160 |
|
| 161 |
Low-Level Access |
| 162 |
^^^^^^^^^^^^^^^^ |
| 163 |
|
| 164 |
At times you may need low-level access to a plugin or file, for example, |
| 165 |
because: |
| 166 |
|
| 167 |
- you wish to have fine-grained control over when the file is opened/closed. |
| 168 |
- you need to perform multiple IO operations and don't want to open the file |
| 169 |
multiple times. |
| 170 |
- a plugin/backend offers unique features not otherwise exposed by the |
| 171 |
high-level API. |
| 172 |
|
| 173 |
For these cases the v3 API offers :func:`iio.v3.imopen |
| 174 |
<imageio.core.imopen.imopen>`. It provides a context manager that initializes |
| 175 |
the plugin and opens the file for reading (``"r"``) or writing (``"w"``), |
| 176 |
similar to the Python built-in function ``open``:: |
| 177 |
|
| 178 |
import imageio.v3 as iio |
| 179 |
|
| 180 |
with iio.imopen("imageio:chelsea.png", "r") as image_file: |
| 181 |
props = image_file.properties() |
| 182 |
# ... configure HPC pipeline and unicorns |
| 183 |
img = image_file.read() |
| 184 |
# image_file.plugin_specific_function() |
| 185 |
|
| 186 |
Similar to above, you can pass the ``plugin`` `kwarg` to imopen to control the |
| 187 |
plugin that is being used. The returned plugin instance (`image_file`) exposes |
| 188 |
the :class:`v3 plugin API <imageio.core.v3_plugin_api.PluginV3>`, and can be |
| 189 |
used for low-level access. |