|
1 |
|
2 :mod:`imghdr` --- Determine the type of an image |
|
3 ================================================ |
|
4 |
|
5 .. module:: imghdr |
|
6 :synopsis: Determine the type of image contained in a file or byte stream. |
|
7 |
|
8 |
|
9 The :mod:`imghdr` module determines the type of image contained in a file or |
|
10 byte stream. |
|
11 |
|
12 The :mod:`imghdr` module defines the following function: |
|
13 |
|
14 |
|
15 .. function:: what(filename[, h]) |
|
16 |
|
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. |
|
20 |
|
21 The following image types are recognized, as listed below with the return value |
|
22 from :func:`what`: |
|
23 |
|
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 +------------+-----------------------------------+ |
|
49 |
|
50 .. versionadded:: 2.5 |
|
51 Exif detection. |
|
52 |
|
53 You can extend the list of file types :mod:`imghdr` can recognize by appending |
|
54 to this variable: |
|
55 |
|
56 |
|
57 .. data:: tests |
|
58 |
|
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``. |
|
62 |
|
63 The test function should return a string describing the image type if the test |
|
64 succeeded, or ``None`` if it failed. |
|
65 |
|
66 Example:: |
|
67 |
|
68 >>> import imghdr |
|
69 >>> imghdr.what('/tmp/bass.gif') |
|
70 'gif' |
|
71 |