| Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 1 | \section{Standard module \sectcode{imghdr}} | 
 | 2 | \stmodindex{imghdr} | 
 | 3 |  | 
 | 4 | The \code{imghdr} module determines the type of image contained in a | 
 | 5 | file or byte stream. | 
 | 6 |  | 
 | 7 | The \code{imghdr} module defines the following function: | 
 | 8 |  | 
 | 9 | \renewcommand{\indexsubitem}{(in module imghdr)} | 
 | 10 |  | 
 | 11 | \begin{funcdesc}{what}{filename\optional{\, h}} | 
 | 12 | Tests the image data contained in the file named by \var{filename}, | 
 | 13 | and returns a string describing the image type.  If optional \var{h} | 
 | 14 | is provided, the \var{filename} is ignored and \var{h} is assumed to | 
 | 15 | contain the byte stream to test. | 
 | 16 | \end{funcdesc} | 
 | 17 |  | 
 | 18 | The following image types are recognized, as listed below with the | 
 | 19 | return 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 |  | 
 | 41 | You can extend the list of file types \code{imghdr} can recognize by | 
 | 42 | appending to this variable: | 
 | 43 |  | 
 | 44 | \begin{datadesc}{tests} | 
 | 45 | A list of functions performing the individual tests.  Each function | 
 | 46 | takes two arguments: the byte-stream and an open file-like object. | 
 | 47 | When \code{what()} is called with a byte-stream, the file-like | 
 | 48 | object will be \code{None}. | 
 | 49 |  | 
 | 50 | The test function should return a string describing the image type if | 
 | 51 | the test succeeded, or \code{None} if it failed. | 
 | 52 | \end{datadesc} | 
 | 53 |  | 
 | 54 | Example: | 
 | 55 |  | 
 | 56 | \begin{verbatim} | 
 | 57 | >>> import imghdr | 
 | 58 | >>> imghdr.what('/tmp/bass.gif') | 
 | 59 | 'gif' | 
 | 60 | \end{verbatim} |