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