blob: 71ef9491e5d994514b1d1e13bf7733787bb15fe1 [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
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
Fred Drake19479911998-02-13 06:58:54 +000010\setindexsubitem{(in module imghdr)}
Guido van Rossum40006cf1996-08-19 22:58:03 +000011
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
Fred Drake19479911998-02-13 06:58:54 +000057\begin{verbatim}
Guido van Rossum40006cf1996-08-19 22:58:03 +000058>>> import imghdr
59>>> imghdr.what('/tmp/bass.gif')
60'gif'
Fred Drake19479911998-02-13 06:58:54 +000061\end{verbatim}