blob: 54e58014876152d34cc6881da0f174524369eb36 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{imghdr} ---
Fred Drakea2e40171999-01-05 22:54:49 +00002 Determine the type of an image.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{imghdr}
4
5\modulesynopsis{Determine the type of image contained in a file or byte stream.}
6
Guido van Rossum40006cf1996-08-19 22:58:03 +00007
Fred Drakebd9ded81998-03-14 20:09:15 +00008The \module{imghdr} module determines the type of image contained in a
Guido van Rossum40006cf1996-08-19 22:58:03 +00009file or byte stream.
10
Fred Drakebd9ded81998-03-14 20:09:15 +000011The \module{imghdr} module defines the following function:
Guido van Rossum40006cf1996-08-19 22:58:03 +000012
Guido van Rossum40006cf1996-08-19 22:58:03 +000013
Fred Drakebd9ded81998-03-14 20:09:15 +000014\begin{funcdesc}{what}{filename\optional{, h}}
Guido van Rossum40006cf1996-08-19 22:58:03 +000015Tests the image data contained in the file named by \var{filename},
16and returns a string describing the image type. If optional \var{h}
17is provided, the \var{filename} is ignored and \var{h} is assumed to
18contain the byte stream to test.
19\end{funcdesc}
20
21The following image types are recognized, as listed below with the
Fred Drakebd9ded81998-03-14 20:09:15 +000022return value from \function{what()}:
Guido van Rossum40006cf1996-08-19 22:58:03 +000023
Fred Drakeee601911998-04-11 20:53:03 +000024\begin{tableii}{l|l}{code}{Value}{Image format}
Fred Drakebd9ded81998-03-14 20:09:15 +000025 \lineii{'rgb'}{SGI ImgLib Files}
26 \lineii{'gif'}{GIF 87a and 89a Files}
27 \lineii{'pbm'}{Portable Bitmap Files}
28 \lineii{'pgm'}{Portable Graymap Files}
29 \lineii{'ppm'}{Portable Pixmap Files}
30 \lineii{'tiff'}{TIFF Files}
31 \lineii{'rast'}{Sun Raster Files}
32 \lineii{'xbm'}{X Bitmap Files}
Guido van Rossum18fa7d21998-10-19 13:30:01 +000033 \lineii{'jpeg'}{JPEG data in JFIF format}
Guido van Rossumdfba2fb1998-07-17 19:01:29 +000034 \lineii{'bmp'}{BMP files}
35 \lineii{'png'}{Portable Network Graphics}
Fred Drakebd9ded81998-03-14 20:09:15 +000036\end{tableii}
Guido van Rossum40006cf1996-08-19 22:58:03 +000037
Fred Drakebd9ded81998-03-14 20:09:15 +000038You can extend the list of file types \module{imghdr} can recognize by
Guido van Rossum40006cf1996-08-19 22:58:03 +000039appending to this variable:
40
41\begin{datadesc}{tests}
42A list of functions performing the individual tests. Each function
43takes two arguments: the byte-stream and an open file-like object.
Fred Drakebd9ded81998-03-14 20:09:15 +000044When \function{what()} is called with a byte-stream, the file-like
Guido van Rossum40006cf1996-08-19 22:58:03 +000045object will be \code{None}.
46
47The test function should return a string describing the image type if
48the test succeeded, or \code{None} if it failed.
49\end{datadesc}
50
51Example:
52
Fred Drake19479911998-02-13 06:58:54 +000053\begin{verbatim}
Guido van Rossum40006cf1996-08-19 22:58:03 +000054>>> import imghdr
55>>> imghdr.what('/tmp/bass.gif')
56'gif'
Fred Drake19479911998-02-13 06:58:54 +000057\end{verbatim}