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