# Creating imageio plugins

Imagio is plugin-based. Every supported format is provided with a plugin. You can write your own plugins to make imageio support additional formats. And we would be interested in adding such code to the imageio codebase!

## What is a plugin

In imageio, a plugin provides one or more `Format` objects, and corresponding `Reader` and `Writer` classes. Each Format object represents an implementation to read/write a particular file format. Its Reader and Writer classes do the actual reading/saving.

The reader and writer objects have a `request` attribute that can be used to obtain information about the read or write `Request`, such as user-provided keyword arguments, as well get access to the raw image data.

## Registering

Strictly speaking a format can be used stand alone. However, to allow imageio to automatically select it for a specific file, the format must be registered using `imageio.formats.add_format()`.

Note that a plugin is not required to be part of the imageio package; as long as a format is registered, imageio can use it. This makes imageio very easy to extend.

## What methods to implement

Imageio is designed such that plugins only need to implement a few private methods. The public API is implemented by the base classes. In effect, the public methods can be given a descent docstring which does not have to be repeated at the plugins.

For the Format class, the following needs to be implemented/specified:

> - The format needs a short name, a description, and a list of file extensions that are common for the file-format in question. These ase set when instantiation the Format object.

> - Use a docstring to provide more detailed information about the format/plugin, such as parameters for reading and saving that the user can supply via keyword arguments.

> - Implement `_can_read(request)`, return a bool. See also the `Request` class.

> - Implement `_can_write(request)`, dito.

For the Format.Reader class:

> - Implement `_open(**kwargs)` to initialize the reader. Deal with the user-provided keyword arguments here.

> - Implement `_close()` to clean up.

> - Implement `_get_length()` to provide a suitable length based on what the user expects. Can be `inf` for streaming data.

> - Implement `_get_data(index)` to return an array and a meta-data dict.

> - Implement `_get_meta_data(index)` to return a meta-data dict. If index is None, it should return the ‘global’ meta-data.

For the Format.Writer class:

> - Implement `_open(**kwargs)` to initialize the writer. Deal with the user-provided keyword arguments here.

> - Implement `_close()` to clean up.

> - Implement `_append_data(im, meta)` to add data (and meta-data).

> - Implement `_set_meta_data(meta)` to set the global meta-data.

If the plugin requires a binary download from the imageio-binaries repository, implement the `download` method (see e.g. the ffmpeg plugin). Make sure that the download directory base name matches the plugin name. Otherwise, the download and removal command line scripts (see __main__.py) might not work.

## Example / template plugin

      1# -*- coding: utf-8 -*-
      2# imageio is distributed under the terms of the (new) BSD License.
      3
      4""" Example plugin. You can use this as a template for your own plugin.
      5"""
      6
      7from __future__ import absolute_import, print_function, division
      8
      9import numpy as np
     10
     11from .. import formats
     12from ..core import Format
     13
     14
     15class DummyFormat(Format):
     16    """ The dummy format is an example format that does nothing.
     17    It will never indicate that it can read or write a file. When
     18    explicitly asked to read, it will simply read the bytes. When
     19    explicitly asked to write, it will raise an error.
     20
     21    This documentation is shown when the user does ``help('thisformat')``.
     22
     23    Parameters for reading
     24    ----------------------
     25    Specify arguments in numpy doc style here.
     26
     27    Parameters for saving
     28    ---------------------
     29    Specify arguments in numpy doc style here.
     30
     31    """
     32
     33    def _can_read(self, request):
     34        # This method is called when the format manager is searching
     35        # for a format to read a certain image. Return True if this format
     36        # can do it.
     37        #
     38        # The format manager is aware of the extensions and the modes
     39        # that each format can handle. It will first ask all formats
     40        # that *seem* to be able to read it whether they can. If none
     41        # can, it will ask the remaining formats if they can: the
     42        # extension might be missing, and this allows formats to provide
     43        # functionality for certain extensions, while giving preference
     44        # to other plugins.
     45        #
     46        # If a format says it can, it should live up to it. The format
     47        # would ideally check the request.firstbytes and look for a
     48        # header of some kind.
     49        #
     50        # The request object has:
     51        # request.filename: a representation of the source (only for reporting)
     52        # request.firstbytes: the first 256 bytes of the file.
     53        # request.mode[0]: read or write mode
     54        # request.mode[1]: what kind of data the user expects: one of 'iIvV?'
     55
     56        if request.mode[1] in (self.modes + "?"):
     57            if request.extension in self.extensions:
     58                return True
     59
     60    def _can_write(self, request):
     61        # This method is called when the format manager is searching
     62        # for a format to write a certain image. It will first ask all
     63        # formats that *seem* to be able to write it whether they can.
     64        # If none can, it will ask the remaining formats if they can.
     65        #
     66        # Return True if the format can do it.
     67
     68        # In most cases, this code does suffice:
     69        if request.mode[1] in (self.modes + "?"):
     70            if request.extension in self.extensions:
     71                return True
     72
     73    # -- reader
     74
     75    class Reader(Format.Reader):
     76        def _open(self, some_option=False, length=1):
     77            # Specify kwargs here. Optionally, the user-specified kwargs
     78            # can also be accessed via the request.kwargs object.
     79            #
     80            # The request object provides two ways to get access to the
     81            # data. Use just one:
     82            #  - Use request.get_file() for a file object (preferred)
     83            #  - Use request.get_local_filename() for a file on the system
     84            self._fp = self.request.get_file()
     85            self._length = length  # passed as an arg in this case for testing
     86            self._data = None
     87
     88        def _close(self):
     89            # Close the reader.
     90            # Note that the request object will close self._fp
     91            pass
     92
     93        def _get_length(self):
     94            # Return the number of images. Can be np.inf
     95            return self._length
     96
     97        def _get_data(self, index):
     98            # Return the data and meta data for the given index
     99            if index >= self._length:
    100                raise IndexError("Image index %i > %i" % (index, self._length))
    101            # Read all bytes
    102            if self._data is None:
    103                self._data = self._fp.read()
    104            # Put in a numpy array
    105            im = np.frombuffer(self._data, "uint8")
    106            im.shape = len(im), 1
    107            # Return array and dummy meta data
    108            return im, {}
    109
    110        def _get_meta_data(self, index):
    111            # Get the meta data for the given index. If index is None, it
    112            # should return the global meta data.
    113            return {}  # This format does not support meta data
    114
    115    # -- writer
    116
    117    class Writer(Format.Writer):
    118        def _open(self, flags=0):
    119            # Specify kwargs here. Optionally, the user-specified kwargs
    120            # can also be accessed via the request.kwargs object.
    121            #
    122            # The request object provides two ways to write the data.
    123            # Use just one:
    124            #  - Use request.get_file() for a file object (preferred)
    125            #  - Use request.get_local_filename() for a file on the system
    126            self._fp = self.request.get_file()
    127
    128        def _close(self):
    129            # Close the reader.
    130            # Note that the request object will close self._fp
    131            pass
    132
    133        def _append_data(self, im, meta):
    134            # Process the given data and meta data.
    135            raise RuntimeError("The dummy format cannot write image data.")
    136
    137        def set_meta_data(self, meta):
    138            # Process the given meta data (global for all images)
    139            # It is not mandatory to support this.
    140            raise RuntimeError("The dummy format cannot write meta data.")
    141
    142
    143# Register. You register an *instance* of a Format class. Here specify:
    144format = DummyFormat(
    145    "dummy",  # short name
    146    "An example format that does nothing.",  # one line descr.
    147    ".foobar .nonexistentext",  # list of extensions
    148    "iI",  # modes, characters in iIvV
    149)
    150formats.add_format(format)