__init__.py 4.78 KB
Newer Older
1 2 3
# import Libraries of other lib packages
import bob.io.base

4 5 6 7
# import our own Library
import bob.extension
bob.extension.load_bob_library('bob.io.image', __file__)

André Anjos's avatar
André Anjos committed
8 9 10 11
from . import _library
from . import version
from .version import module as __version__

12
from ._library import *
13
from .utils import *
14

15 16
import os

André Anjos's avatar
André Anjos committed
17 18 19
def get_config():
  """Returns a string containing the configuration information.
  """
20 21
  import bob.extension
  return bob.extension.get_config(__name__, version.externals)
André Anjos's avatar
André Anjos committed
22

23
def load(filename, extension=None):
Manuel Günther's avatar
Manuel Günther committed
24
  """load(filename, extension) -> image
25 26 27 28 29

  This function loads and image from the file with the specified ``filename``.
  The type of the image will be determined based on the ``extension`` parameter, which can have the following values:

  - ``None``: The file name extension of the ``filename`` is used to determine the image type.
Manuel Günther's avatar
Manuel Günther committed
30 31
  - ``'auto'``: The type of the image will be detected automatically, using :py:func:`bob.io.image.get_correct_image_extension`.
  - ``'.xxx'``: The image type is determined by the given extension.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
                For a list of possible extensions, see :py:func:`bob.io.base.extensions` (only the image extensions are valid here).

  **Parameters:**

  ``filename`` : str
    The name of the image file to load.

  ``extension`` : str
    [Default: ``None``] If given, the given extension will determine the type of the image.
    Use ``'auto'`` to automatically determine the extension (this might take slightly more time).

  **Returns**

  ``image`` : 2D or 3D :py:class:`numpy.ndarray` of type ``uint8``
    The image read from the specified file.
  """
  # check the extension
  if extension is None:
    f = bob.io.base.File(filename, 'r')
  else:
    if extension == 'auto':
53
      extension = get_correct_image_extension(filename)
54 55 56 57 58 59 60
    f = bob.io.base.File(filename, 'r', extension)

  return f.read()

# use the same alias as for bob.io.base.load
read = load

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
def get_include_directories():
  """get_include_directories() -> includes

  Returns a list of include directories for dependent libraries, such as libjpeg, libtiff, ...
  This function is automatically used by :py:func:`bob.extension.get_bob_libraries` to retrieve the non-standard include directories that are required to use the C bindings of this library in dependent classes.
  You shouldn't normally need to call this function by hand.

  **Returns:**

  ``includes`` : [str]
    The list of non-standard include directories required to use the C bindings of this class.
    For now, only the directory for the HDF5 headers are returned.
  """
  # try to use pkg_config first
  from bob.extension.utils import find_header, uniq_paths
  from bob.extension import pkgconfig
  import logging
  logger = logging.getLogger("bob.io.image")
  directories = []
  for name, header in (('libjpeg', 'jpeglib.h'), ('libtiff', 'tiff.h'), ('giflib', 'gif_lib.h')):
    # locate pkg-config on our own
    candidates = find_header(header)
    if not candidates:
      logger.warn("could not find %s's `%s' - have you installed %s on this machine?" % (name, header, name))

    directories.append(os.path.dirname(candidates[0]))
  for name in ("libpng",):
Manuel Günther's avatar
Manuel Günther committed
88 89 90 91 92
    try:
      pkg = pkgconfig(name)
      directories.extend(pkg.include_directories())
    except:
      pass
93 94 95
  return uniq_paths(directories)


Manuel Günther's avatar
Manuel Günther committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
def get_macros():
  """get_macros() -> macros

  Returns a list of preprocessor macros, such as ``(HAVE_LIBJPEG, 1)``.
  This function is automatically used by :py:func:`bob.extension.get_bob_libraries` to retrieve the prerpocessor definitions that are required to use the C bindings of this library in dependent classes.
  You shouldn't normally need to call this function by hand.

  **Returns:**

  ``macros`` : [str]
    The list of preprocessor macros required to use the C bindings of this class.
  """
  # try to use pkg_config first
  from bob.extension.utils import find_header, uniq_paths
  from bob.extension import pkgconfig
  macros = []
Manuel Günther's avatar
Manuel Günther committed
112
  for define, header in (('HAVE_LIBJPEG', 'jpeglib.h'), ('HAVE_LIBTIFF', 'tiff.h'), ('HAVE_GIFLIB', 'gif_lib.h')):
Manuel Günther's avatar
Manuel Günther committed
113 114 115 116
    # locate pkg-config on our own
    candidates = find_header(header)
    if candidates:
      macros.append((define, '1'))
Manuel Günther's avatar
Manuel Günther committed
117
  for define, name in (("HAVE_LIBPNG", "libpng"),):
Manuel Günther's avatar
Manuel Günther committed
118 119 120 121 122 123 124 125
    try:
      pkg = pkgconfig(name)
      macros.append((define, '1'))
    except:
      pass
  return macros


126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
# gets sphinx autodoc done right - don't remove it
def __appropriate__(*args):
  """Says object was actually declared here, an not on the import module.

  Parameters:

    *args: An iterable of objects to modify

  Resolves `Sphinx referencing issues
  <https://github.com/sphinx-doc/sphinx/issues/3048>`
  """

  for obj in args:
    obj.__module__ = __name__


__appropriate__(
    imshow,
    to_matplotlib,
  )

André Anjos's avatar
André Anjos committed
147 148
# gets sphinx autodoc done right - don't remove it
__all__ = [_ for _ in dir() if not _.startswith('_')]