| 1 | \section{\module{imghdr} ---
|
|---|
| 2 | Determine the type of an image}
|
|---|
| 3 |
|
|---|
| 4 | \declaremodule{standard}{imghdr}
|
|---|
| 5 | \modulesynopsis{Determine the type of image contained in a file or
|
|---|
| 6 | byte stream.}
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 | The \module{imghdr} module determines the type of image contained in a
|
|---|
| 10 | file or byte stream.
|
|---|
| 11 |
|
|---|
| 12 | The \module{imghdr} module defines the following function:
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 | \begin{funcdesc}{what}{filename\optional{, h}}
|
|---|
| 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
|
|---|
| 23 | return value from \function{what()}:
|
|---|
| 24 |
|
|---|
| 25 | \begin{tableii}{l|l}{code}{Value}{Image format}
|
|---|
| 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}
|
|---|
| 34 | \lineii{'jpeg'}{JPEG data in JFIF or Exif formats}
|
|---|
| 35 | \lineii{'bmp'}{BMP files}
|
|---|
| 36 | \lineii{'png'}{Portable Network Graphics}
|
|---|
| 37 | \end{tableii}
|
|---|
| 38 |
|
|---|
| 39 | \versionadded[Exif detection]{2.5}
|
|---|
| 40 |
|
|---|
| 41 | You can extend the list of file types \module{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 \function{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}
|
|---|