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