blob: 0c0722df17ee01d1559d766ad8d612520925d14d [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001: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
8The :mod:`imghdr` module determines the type of image contained in a file or
9byte stream.
10
11The :mod:`imghdr` module defines the following function:
12
13
Georg Brandl3dd33882009-06-01 17:35:27 +000014.. function:: what(filename, h=None)
Georg Brandl116aa622007-08-15 14:28:22 +000015
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
20The following image types are recognized, as listed below with the return value
21from :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 Brandl116aa622007-08-15 14:28:22 +000049You can extend the list of file types :mod:`imghdr` can recognize by appending
50to 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
62Example::
63
64 >>> import imghdr
65 >>> imghdr.what('/tmp/bass.gif')
66 'gif'
67