Issue #5262: Improved fix.
[python.git] / Doc / library / imghdr.rst
blob90a83045c394c4e875c7c9481c16832098c12fdc
2 :mod:`imghdr` --- Determine the type of an image
3 ================================================
5 .. module:: imghdr
6    :synopsis: Determine the type of image contained in a file or byte stream.
9 The :mod:`imghdr` module determines the type of image contained in a file or
10 byte stream.
12 The :mod:`imghdr` module defines the following function:
15 .. function:: what(filename[, h])
17    Tests the image data contained in the file named by *filename*, and returns a
18    string describing the image type.  If optional *h* is provided, the *filename*
19    is ignored and *h* is assumed to contain the byte stream to test.
21 The following image types are recognized, as listed below with the return value
22 from :func:`what`:
24 +------------+-----------------------------------+
25 | Value      | Image format                      |
26 +============+===================================+
27 | ``'rgb'``  | SGI ImgLib Files                  |
28 +------------+-----------------------------------+
29 | ``'gif'``  | GIF 87a and 89a Files             |
30 +------------+-----------------------------------+
31 | ``'pbm'``  | Portable Bitmap Files             |
32 +------------+-----------------------------------+
33 | ``'pgm'``  | Portable Graymap Files            |
34 +------------+-----------------------------------+
35 | ``'ppm'``  | Portable Pixmap Files             |
36 +------------+-----------------------------------+
37 | ``'tiff'`` | TIFF Files                        |
38 +------------+-----------------------------------+
39 | ``'rast'`` | Sun Raster Files                  |
40 +------------+-----------------------------------+
41 | ``'xbm'``  | X Bitmap Files                    |
42 +------------+-----------------------------------+
43 | ``'jpeg'`` | JPEG data in JFIF or Exif formats |
44 +------------+-----------------------------------+
45 | ``'bmp'``  | BMP files                         |
46 +------------+-----------------------------------+
47 | ``'png'``  | Portable Network Graphics         |
48 +------------+-----------------------------------+
50 .. versionadded:: 2.5
51    Exif detection.
53 You can extend the list of file types :mod:`imghdr` can recognize by appending
54 to this variable:
57 .. data:: tests
59    A list of functions performing the individual tests.  Each function takes two
60    arguments: the byte-stream and an open file-like object. When :func:`what` is
61    called with a byte-stream, the file-like object will be ``None``.
63    The test function should return a string describing the image type if the test
64    succeeded, or ``None`` if it failed.
66 Example::
68    >>> import imghdr
69    >>> imghdr.what('/tmp/bass.gif')
70    'gif'