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