Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{imghdr} --- |
Fred Drake | f8ca7d8 | 2000-10-10 17:03:45 +0000 | [diff] [blame] | 2 | Determine the type of an image} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | f8ca7d8 | 2000-10-10 17:03:45 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{imghdr} |
| 5 | \modulesynopsis{Determine the type of image contained in a file or |
| 6 | byte stream.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 8 | |
Fred Drake | bd9ded8 | 1998-03-14 20:09:15 +0000 | [diff] [blame] | 9 | 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] | 10 | file or byte stream. |
| 11 | |
Fred Drake | bd9ded8 | 1998-03-14 20:09:15 +0000 | [diff] [blame] | 12 | The \module{imghdr} module defines the following function: |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 14 | |
Fred Drake | bd9ded8 | 1998-03-14 20:09:15 +0000 | [diff] [blame] | 15 | \begin{funcdesc}{what}{filename\optional{, h}} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 16 | Tests the image data contained in the file named by \var{filename}, |
| 17 | and returns a string describing the image type. If optional \var{h} |
| 18 | is provided, the \var{filename} is ignored and \var{h} is assumed to |
| 19 | contain the byte stream to test. |
| 20 | \end{funcdesc} |
| 21 | |
| 22 | The following image types are recognized, as listed below with the |
Fred Drake | bd9ded8 | 1998-03-14 20:09:15 +0000 | [diff] [blame] | 23 | return value from \function{what()}: |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 24 | |
Fred Drake | ee60191 | 1998-04-11 20:53:03 +0000 | [diff] [blame] | 25 | \begin{tableii}{l|l}{code}{Value}{Image format} |
Fred Drake | bd9ded8 | 1998-03-14 20:09:15 +0000 | [diff] [blame] | 26 | \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 Hettinger | 97db05d | 2005-01-07 08:15:41 +0000 | [diff] [blame^] | 34 | \lineii{'jpeg'}{JPEG data in JFIF or Exif formats} |
Guido van Rossum | dfba2fb | 1998-07-17 19:01:29 +0000 | [diff] [blame] | 35 | \lineii{'bmp'}{BMP files} |
| 36 | \lineii{'png'}{Portable Network Graphics} |
Fred Drake | bd9ded8 | 1998-03-14 20:09:15 +0000 | [diff] [blame] | 37 | \end{tableii} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 38 | |
Raymond Hettinger | 97db05d | 2005-01-07 08:15:41 +0000 | [diff] [blame^] | 39 | \versionadded[Exif detection]{2.5} |
| 40 | |
Fred Drake | bd9ded8 | 1998-03-14 20:09:15 +0000 | [diff] [blame] | 41 | 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] | 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. |
Fred Drake | bd9ded8 | 1998-03-14 20:09:15 +0000 | [diff] [blame] | 47 | 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] | 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 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 56 | \begin{verbatim} |
Guido van Rossum | 40006cf | 1996-08-19 22:58:03 +0000 | [diff] [blame] | 57 | >>> import imghdr |
| 58 | >>> imghdr.what('/tmp/bass.gif') |
| 59 | 'gif' |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 60 | \end{verbatim} |