blob: d533a3e56ba75dae171af9b0fd6291bacb06ea5c [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
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
9The :mod:`imghdr` module determines the type of image contained in a file or
10byte stream.
11
12The :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
21The following image types are recognized, as listed below with the return value
22from :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
Georg Brandl116aa622007-08-15 14:28:22 +000050You can extend the list of file types :mod:`imghdr` can recognize by appending
51to this variable:
52
53
54.. data:: tests
55
56 A list of functions performing the individual tests. Each function takes two
57 arguments: the byte-stream and an open file-like object. When :func:`what` is
58 called with a byte-stream, the file-like object will be ``None``.
59
60 The test function should return a string describing the image type if the test
61 succeeded, or ``None`` if it failed.
62
63Example::
64
65 >>> import imghdr
66 >>> imghdr.what('/tmp/bass.gif')
67 'gif'
68