Fred Drake | 3a0351c | 1998-04-04 07:23:21 +0000 | [diff] [blame] | 1 | \section{Standard Module \module{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 | |
Fred Drake | bd9ded8 | 1998-03-14 20:09:15 +0000 | [diff] [blame] | 5 | The \module{imghdr} module determines the type of image contained in a |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 6 | file or byte stream. |
| 7 | |
Fred Drake | bd9ded8 | 1998-03-14 20:09:15 +0000 | [diff] [blame] | 8 | The \module{imghdr} module defines the following function: |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 10 | |
Fred Drake | bd9ded8 | 1998-03-14 20:09:15 +0000 | [diff] [blame] | 11 | \begin{funcdesc}{what}{filename\optional{, h}} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 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 |
Fred Drake | bd9ded8 | 1998-03-14 20:09:15 +0000 | [diff] [blame] | 19 | return value from \function{what()}: |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 20 | |
Fred Drake | bd9ded8 | 1998-03-14 20:09:15 +0000 | [diff] [blame] | 21 | \begin{tableii}{|l|l|}{code}{Value}{Image format} |
| 22 | \lineii{'rgb'}{SGI ImgLib Files} |
| 23 | \lineii{'gif'}{GIF 87a and 89a Files} |
| 24 | \lineii{'pbm'}{Portable Bitmap Files} |
| 25 | \lineii{'pgm'}{Portable Graymap Files} |
| 26 | \lineii{'ppm'}{Portable Pixmap Files} |
| 27 | \lineii{'tiff'}{TIFF Files} |
| 28 | \lineii{'rast'}{Sun Raster Files} |
| 29 | \lineii{'xbm'}{X Bitmap Files} |
| 30 | \lineii{'jpeg'}{JPEG data in JIFF format} |
| 31 | \end{tableii} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 32 | |
Fred Drake | bd9ded8 | 1998-03-14 20:09:15 +0000 | [diff] [blame] | 33 | You can extend the list of file types \module{imghdr} can recognize by |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 34 | appending to this variable: |
| 35 | |
| 36 | \begin{datadesc}{tests} |
| 37 | A list of functions performing the individual tests. Each function |
| 38 | takes two arguments: the byte-stream and an open file-like object. |
Fred Drake | bd9ded8 | 1998-03-14 20:09:15 +0000 | [diff] [blame] | 39 | When \function{what()} is called with a byte-stream, the file-like |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 40 | object will be \code{None}. |
| 41 | |
| 42 | The test function should return a string describing the image type if |
| 43 | the test succeeded, or \code{None} if it failed. |
| 44 | \end{datadesc} |
| 45 | |
| 46 | Example: |
| 47 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 48 | \begin{verbatim} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 49 | >>> import imghdr |
| 50 | >>> imghdr.what('/tmp/bass.gif') |
| 51 | 'gif' |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 52 | \end{verbatim} |