blob: 4a4f36855d9b5f49c994819a05112b1ef005aeaa [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}
Raymond Hettinger97db05d2005-01-07 08:15:41 +000034 \lineii{'jpeg'}{JPEG data in JFIF or Exif formats}
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
Raymond Hettinger97db05d2005-01-07 08:15:41 +000039\versionadded[Exif detection]{2.5}
40
Fred Drakebd9ded81998-03-14 20:09:15 +000041You can extend the list of file types \module{imghdr} can recognize by
Guido van Rossum40006cf1996-08-19 22:58:03 +000042appending 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.
Fred Drakebd9ded81998-03-14 20:09:15 +000047When \function{what()} is called with a byte-stream, the file-like
Guido van Rossum40006cf1996-08-19 22:58:03 +000048object 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
Fred Drake19479911998-02-13 06:58:54 +000056\begin{verbatim}
Guido van Rossum40006cf1996-08-19 22:58:03 +000057>>> import imghdr
58>>> imghdr.what('/tmp/bass.gif')
59'gif'
Fred Drake19479911998-02-13 06:58:54 +000060\end{verbatim}