blob: bf1c0ef4e06e4c8d5184ebb150e0e0733d72449f [file] [log] [blame]
Fred Drakebcda4841997-12-17 14:01:52 +00001\section{Standard Module \sectcode{imghdr}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-imghdr}
Guido van Rossum40006cf1996-08-19 22:58:03 +00003\stmodindex{imghdr}
4
Fred Drakebd9ded81998-03-14 20:09:15 +00005The \module{imghdr} module determines the type of image contained in a
Guido van Rossum40006cf1996-08-19 22:58:03 +00006file or byte stream.
7
Fred Drakebd9ded81998-03-14 20:09:15 +00008The \module{imghdr} module defines the following function:
Guido van Rossum40006cf1996-08-19 22:58:03 +00009
Guido van Rossum40006cf1996-08-19 22:58:03 +000010
Fred Drakebd9ded81998-03-14 20:09:15 +000011\begin{funcdesc}{what}{filename\optional{, h}}
Guido van Rossum40006cf1996-08-19 22:58:03 +000012Tests the image data contained in the file named by \var{filename},
13and returns a string describing the image type. If optional \var{h}
14is provided, the \var{filename} is ignored and \var{h} is assumed to
15contain the byte stream to test.
16\end{funcdesc}
17
18The following image types are recognized, as listed below with the
Fred Drakebd9ded81998-03-14 20:09:15 +000019return value from \function{what()}:
Guido van Rossum40006cf1996-08-19 22:58:03 +000020
Fred Drakebd9ded81998-03-14 20:09:15 +000021\begin{tableii}{|l|l|}{code}{Value}{Image format}
22 \lineii{'rgb'}{SGI ImgLib Files}
23 \lineii{'gif'}{GIF 87a and 89a Files}
24 \lineii{'pbm'}{Portable Bitmap Files}
25 \lineii{'pgm'}{Portable Graymap Files}
26 \lineii{'ppm'}{Portable Pixmap Files}
27 \lineii{'tiff'}{TIFF Files}
28 \lineii{'rast'}{Sun Raster Files}
29 \lineii{'xbm'}{X Bitmap Files}
30 \lineii{'jpeg'}{JPEG data in JIFF format}
31\end{tableii}
Guido van Rossum40006cf1996-08-19 22:58:03 +000032
Fred Drakebd9ded81998-03-14 20:09:15 +000033You can extend the list of file types \module{imghdr} can recognize by
Guido van Rossum40006cf1996-08-19 22:58:03 +000034appending to this variable:
35
36\begin{datadesc}{tests}
37A list of functions performing the individual tests. Each function
38takes two arguments: the byte-stream and an open file-like object.
Fred Drakebd9ded81998-03-14 20:09:15 +000039When \function{what()} is called with a byte-stream, the file-like
Guido van Rossum40006cf1996-08-19 22:58:03 +000040object will be \code{None}.
41
42The test function should return a string describing the image type if
43the test succeeded, or \code{None} if it failed.
44\end{datadesc}
45
46Example:
47
Fred Drake19479911998-02-13 06:58:54 +000048\begin{verbatim}
Guido van Rossum40006cf1996-08-19 22:58:03 +000049>>> import imghdr
50>>> imghdr.what('/tmp/bass.gif')
51'gif'
Fred Drake19479911998-02-13 06:58:54 +000052\end{verbatim}