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