Commit 28d836f2 authored by Manuel Günther's avatar Manuel Günther
Browse files

Added function that is able to automatically detect the...

Added function that is able to automatically detect the image type (using imghdr.what)
parent 10a67d72
......@@ -18,6 +18,45 @@ def get_config():
return bob.extension.get_config(__name__, version.externals)
def load(filename, extension=None):
"""load(filename) -> image
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.
- ``'auto'``: The type of the image will be detected automatically, using the :py:mod:`imghdr` module.
- ``'.xxx`'': The image type is determined by the given extension.
For a list of possible extensions, see :py:func:`` (only the image extensions are valid here).
``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).
``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 =, 'r')
if extension == 'auto':
import imghdr
extension = "." + imghdr.what(filename)
f =, 'r', extension)
# use the same alias as for
read = load
def get_include_directories():
"""get_include_directories() -> includes
......@@ -11,6 +11,8 @@
import os
import numpy
from import load, write, test_utils
import nose
# These are some global parameters for the test.
PNG_INDEXED_COLOR = test_utils.datafile('img_indexed_color.png', __name__)
......@@ -67,6 +69,24 @@ def test_netpbm():
# because of re-compression
def test_image_load():
# test that the generic function works as expected
for filename in ('test.jpg', 'test.pbm', 'test.pgm', 'test.ppm', 'img_rgba_color.png'):
full_file = test_utils.datafile(filename, __name__)
# load with just image name
i1 =
# load with image name and extension
i2 =, os.path.splitext(full_file)[1])
assert numpy.array_equal(i1,i2)
# load with image name and automatically estimated extension
i3 =, 'auto')
assert numpy.array_equal(i1,i3)
# assert that unknown extensions raise exceptions, lambda x:, ".unknown"), full_file)
def test_cpp_interface():
from ._library import _test_io
import tempfile
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment