Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{imghdr} --- |
Fred Drake | a2e4017 | 1999-01-05 22:54:49 +0000 | [diff] [blame] | 2 | Determine the type of an image.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | \declaremodule{standard}{imghdr} |
| 4 | |
| 5 | \modulesynopsis{Determine the type of image contained in a file or byte stream.} |
| 6 | |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 7 | |
Fred Drake | bd9ded8 | 1998-03-14 20:09:15 +0000 | [diff] [blame] | 8 | 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] | 9 | file or byte stream. |
| 10 | |
Fred Drake | bd9ded8 | 1998-03-14 20:09:15 +0000 | [diff] [blame] | 11 | The \module{imghdr} module defines the following function: |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 13 | |
Fred Drake | bd9ded8 | 1998-03-14 20:09:15 +0000 | [diff] [blame] | 14 | \begin{funcdesc}{what}{filename\optional{, h}} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 15 | Tests the image data contained in the file named by \var{filename}, |
| 16 | and returns a string describing the image type. If optional \var{h} |
| 17 | is provided, the \var{filename} is ignored and \var{h} is assumed to |
| 18 | contain the byte stream to test. |
| 19 | \end{funcdesc} |
| 20 | |
| 21 | The following image types are recognized, as listed below with the |
Fred Drake | bd9ded8 | 1998-03-14 20:09:15 +0000 | [diff] [blame] | 22 | return value from \function{what()}: |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 23 | |
Fred Drake | ee60191 | 1998-04-11 20:53:03 +0000 | [diff] [blame] | 24 | \begin{tableii}{l|l}{code}{Value}{Image format} |
Fred Drake | bd9ded8 | 1998-03-14 20:09:15 +0000 | [diff] [blame] | 25 | \lineii{'rgb'}{SGI ImgLib Files} |
| 26 | \lineii{'gif'}{GIF 87a and 89a Files} |
| 27 | \lineii{'pbm'}{Portable Bitmap Files} |
| 28 | \lineii{'pgm'}{Portable Graymap Files} |
| 29 | \lineii{'ppm'}{Portable Pixmap Files} |
| 30 | \lineii{'tiff'}{TIFF Files} |
| 31 | \lineii{'rast'}{Sun Raster Files} |
| 32 | \lineii{'xbm'}{X Bitmap Files} |
Guido van Rossum | 18fa7d2 | 1998-10-19 13:30:01 +0000 | [diff] [blame] | 33 | \lineii{'jpeg'}{JPEG data in JFIF format} |
Guido van Rossum | dfba2fb | 1998-07-17 19:01:29 +0000 | [diff] [blame] | 34 | \lineii{'bmp'}{BMP files} |
| 35 | \lineii{'png'}{Portable Network Graphics} |
Fred Drake | bd9ded8 | 1998-03-14 20:09:15 +0000 | [diff] [blame] | 36 | \end{tableii} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 37 | |
Fred Drake | bd9ded8 | 1998-03-14 20:09:15 +0000 | [diff] [blame] | 38 | 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] | 39 | appending to this variable: |
| 40 | |
| 41 | \begin{datadesc}{tests} |
| 42 | A list of functions performing the individual tests. Each function |
| 43 | takes two arguments: the byte-stream and an open file-like object. |
Fred Drake | bd9ded8 | 1998-03-14 20:09:15 +0000 | [diff] [blame] | 44 | 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] | 45 | object will be \code{None}. |
| 46 | |
| 47 | The test function should return a string describing the image type if |
| 48 | the test succeeded, or \code{None} if it failed. |
| 49 | \end{datadesc} |
| 50 | |
| 51 | Example: |
| 52 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 53 | \begin{verbatim} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 54 | >>> import imghdr |
| 55 | >>> imghdr.what('/tmp/bass.gif') |
| 56 | 'gif' |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 57 | \end{verbatim} |