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