blob: 0dec8a16bfec0b2d84f81c8cbaaf5cdd64a3a371 [file] [log] [blame]
Guido van Rossum40006cf1996-08-19 22:58:03 +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
5The \code{imghdr} module determines the type of image contained in a
6file or byte stream.
7
8The \code{imghdr} module defines the following function:
9
10\renewcommand{\indexsubitem}{(in module imghdr)}
11
12\begin{funcdesc}{what}{filename\optional{\, h}}
13Tests the image data contained in the file named by \var{filename},
14and returns a string describing the image type. If optional \var{h}
15is provided, the \var{filename} is ignored and \var{h} is assumed to
16contain the byte stream to test.
17\end{funcdesc}
18
19The following image types are recognized, as listed below with the
20return value from \code{what}:
21
22\begin{enumerate}
23\item[``rgb''] SGI ImgLib Files
24
25\item[``gif''] GIF 87a and 89a Files
26
27\item[``pbm''] Portable Bitmap Files
28
29\item[``pgm''] Portable Graymap Files
30
31\item[``ppm''] Portable Pixmap Files
32
33\item[``tiff''] TIFF Files
34
35\item[``rast''] Sun Raster Files
36
37\item[``xbm''] X Bitmap Files
38
39\item[``jpeg''] JPEG data in JIFF format
40\end{enumerate}
41
42You can extend the list of file types \code{imghdr} can recognize by
43appending to this variable:
44
45\begin{datadesc}{tests}
46A list of functions performing the individual tests. Each function
47takes two arguments: the byte-stream and an open file-like object.
48When \code{what()} is called with a byte-stream, the file-like
49object will be \code{None}.
50
51The test function should return a string describing the image type if
52the test succeeded, or \code{None} if it failed.
53\end{datadesc}
54
55Example:
56
Guido van Rossume47da0a1997-07-17 16:34:52 +000057\bcode\begin{verbatim}
Guido van Rossum40006cf1996-08-19 22:58:03 +000058>>> import imghdr
59>>> imghdr.what('/tmp/bass.gif')
60'gif'
Guido van Rossume47da0a1997-07-17 16:34:52 +000061\end{verbatim}\ecode