blob: 2f1ac7d9f0f189ba230b43f8abbae7f963a3f6ec [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{imghdr} ---
Fred Drakef8ca7d82000-10-10 17:03:45 +00002 Determine the type of an image}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drakef8ca7d82000-10-10 17:03:45 +00004\declaremodule{standard}{imghdr}
5\modulesynopsis{Determine the type of image contained in a file or
6 byte stream.}
Fred Drakeb91e9341998-07-23 17:59:49 +00007
Guido van Rossum40006cf1996-08-19 22:58:03 +00008
Fred Drakebd9ded81998-03-14 20:09:15 +00009The \module{imghdr} module determines the type of image contained in a
Guido van Rossum40006cf1996-08-19 22:58:03 +000010file or byte stream.
11
Fred Drakebd9ded81998-03-14 20:09:15 +000012The \module{imghdr} module defines the following function:
Guido van Rossum40006cf1996-08-19 22:58:03 +000013
Guido van Rossum40006cf1996-08-19 22:58:03 +000014
Fred Drakebd9ded81998-03-14 20:09:15 +000015\begin{funcdesc}{what}{filename\optional{, h}}
Guido van Rossum40006cf1996-08-19 22:58:03 +000016Tests the image data contained in the file named by \var{filename},
17and returns a string describing the image type. If optional \var{h}
18is provided, the \var{filename} is ignored and \var{h} is assumed to
19contain the byte stream to test.
20\end{funcdesc}
21
22The following image types are recognized, as listed below with the
Fred Drakebd9ded81998-03-14 20:09:15 +000023return value from \function{what()}:
Guido van Rossum40006cf1996-08-19 22:58:03 +000024
Fred Drakeee601911998-04-11 20:53:03 +000025\begin{tableii}{l|l}{code}{Value}{Image format}
Fred Drakebd9ded81998-03-14 20:09:15 +000026 \lineii{'rgb'}{SGI ImgLib Files}
27 \lineii{'gif'}{GIF 87a and 89a Files}
28 \lineii{'pbm'}{Portable Bitmap Files}
29 \lineii{'pgm'}{Portable Graymap Files}
30 \lineii{'ppm'}{Portable Pixmap Files}
31 \lineii{'tiff'}{TIFF Files}
32 \lineii{'rast'}{Sun Raster Files}
33 \lineii{'xbm'}{X Bitmap Files}
Guido van Rossum18fa7d21998-10-19 13:30:01 +000034 \lineii{'jpeg'}{JPEG data in JFIF format}
Guido van Rossumdfba2fb1998-07-17 19:01:29 +000035 \lineii{'bmp'}{BMP files}
36 \lineii{'png'}{Portable Network Graphics}
Fred Drakebd9ded81998-03-14 20:09:15 +000037\end{tableii}
Guido van Rossum40006cf1996-08-19 22:58:03 +000038
Fred Drakebd9ded81998-03-14 20:09:15 +000039You can extend the list of file types \module{imghdr} can recognize by
Guido van Rossum40006cf1996-08-19 22:58:03 +000040appending to this variable:
41
42\begin{datadesc}{tests}
43A list of functions performing the individual tests. Each function
44takes two arguments: the byte-stream and an open file-like object.
Fred Drakebd9ded81998-03-14 20:09:15 +000045When \function{what()} is called with a byte-stream, the file-like
Guido van Rossum40006cf1996-08-19 22:58:03 +000046object will be \code{None}.
47
48The test function should return a string describing the image type if
49the test succeeded, or \code{None} if it failed.
50\end{datadesc}
51
52Example:
53
Fred Drake19479911998-02-13 06:58:54 +000054\begin{verbatim}
Guido van Rossum40006cf1996-08-19 22:58:03 +000055>>> import imghdr
56>>> imghdr.what('/tmp/bass.gif')
57'gif'
Fred Drake19479911998-02-13 06:58:54 +000058\end{verbatim}