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 | |
Raymond Hettinger | 469271d | 2011-01-27 20:38:46 +0000 | [diff] [blame] | 7 | **Source code:** :source:`Lib/imghdr.py` |
| 8 | |
| 9 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 10 | |
| 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 | |
Georg Brandl | 3dd3388 | 2009-06-01 17:35:27 +0000 | [diff] [blame] | 17 | .. function:: what(filename, h=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 | |
Berker Peksag | ef223a1 | 2016-10-01 05:01:54 +0300 | [diff] [blame] | 23 | .. versionchanged:: 3.6 |
| 24 | Accepts a :term:`path-like object`. |
| 25 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 | The following image types are recognized, as listed below with the return value |
| 27 | from :func:`what`: |
| 28 | |
| 29 | +------------+-----------------------------------+ |
| 30 | | Value | Image format | |
| 31 | +============+===================================+ |
| 32 | | ``'rgb'`` | SGI ImgLib Files | |
| 33 | +------------+-----------------------------------+ |
| 34 | | ``'gif'`` | GIF 87a and 89a Files | |
| 35 | +------------+-----------------------------------+ |
| 36 | | ``'pbm'`` | Portable Bitmap Files | |
| 37 | +------------+-----------------------------------+ |
| 38 | | ``'pgm'`` | Portable Graymap Files | |
| 39 | +------------+-----------------------------------+ |
| 40 | | ``'ppm'`` | Portable Pixmap Files | |
| 41 | +------------+-----------------------------------+ |
| 42 | | ``'tiff'`` | TIFF Files | |
| 43 | +------------+-----------------------------------+ |
| 44 | | ``'rast'`` | Sun Raster Files | |
| 45 | +------------+-----------------------------------+ |
| 46 | | ``'xbm'`` | X Bitmap Files | |
| 47 | +------------+-----------------------------------+ |
| 48 | | ``'jpeg'`` | JPEG data in JFIF or Exif formats | |
| 49 | +------------+-----------------------------------+ |
| 50 | | ``'bmp'`` | BMP files | |
| 51 | +------------+-----------------------------------+ |
| 52 | | ``'png'`` | Portable Network Graphics | |
| 53 | +------------+-----------------------------------+ |
Serhiy Storchaka | 2f8dca7 | 2014-05-25 11:45:37 +0300 | [diff] [blame] | 54 | | ``'webp'`` | WebP files | |
| 55 | +------------+-----------------------------------+ |
R David Murray | 2f60820 | 2014-06-26 12:27:57 -0400 | [diff] [blame] | 56 | | ``'exr'`` | OpenEXR Files | |
| 57 | +------------+-----------------------------------+ |
| 58 | |
| 59 | .. versionadded:: 3.5 |
Yury Selivanov | 100fc3f | 2015-09-08 22:40:30 -0400 | [diff] [blame] | 60 | The *exr* and *webp* formats were added. |
Serhiy Storchaka | 2f8dca7 | 2014-05-25 11:45:37 +0300 | [diff] [blame] | 61 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 62 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 63 | You can extend the list of file types :mod:`imghdr` can recognize by appending |
| 64 | to this variable: |
| 65 | |
| 66 | |
| 67 | .. data:: tests |
| 68 | |
| 69 | A list of functions performing the individual tests. Each function takes two |
| 70 | arguments: the byte-stream and an open file-like object. When :func:`what` is |
| 71 | called with a byte-stream, the file-like object will be ``None``. |
| 72 | |
| 73 | The test function should return a string describing the image type if the test |
| 74 | succeeded, or ``None`` if it failed. |
| 75 | |
| 76 | Example:: |
| 77 | |
| 78 | >>> import imghdr |
Petri Lehtinen | 9f74c6c | 2013-02-23 19:26:56 +0100 | [diff] [blame] | 79 | >>> imghdr.what('bass.gif') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 80 | 'gif' |
| 81 | |