Staging
v0.5.1
https://github.com/python/cpython
Raw File
Tip revision: cf6fc7906d5abb6e0b9e7e52c94e8b28883672f7 authored by cvs2svn on 05 August 2004, 07:21:01 UTC
This commit was manufactured by cvs2svn to create tag 'r24a2'.
Tip revision: cf6fc79
libimghdr.tex
\section{\module{imghdr} ---
         Determine the type of an image}

\declaremodule{standard}{imghdr}
\modulesynopsis{Determine the type of image contained in a file or
                byte stream.}


The \module{imghdr} module determines the type of image contained in a
file or byte stream.

The \module{imghdr} module defines the following function:


\begin{funcdesc}{what}{filename\optional{, h}}
Tests the image data contained in the file named by \var{filename},
and returns a string describing the image type.  If optional \var{h}
is provided, the \var{filename} is ignored and \var{h} is assumed to
contain the byte stream to test.
\end{funcdesc}

The following image types are recognized, as listed below with the
return value from \function{what()}:

\begin{tableii}{l|l}{code}{Value}{Image format}
  \lineii{'rgb'}{SGI ImgLib Files}
  \lineii{'gif'}{GIF 87a and 89a Files}
  \lineii{'pbm'}{Portable Bitmap Files}
  \lineii{'pgm'}{Portable Graymap Files}
  \lineii{'ppm'}{Portable Pixmap Files}
  \lineii{'tiff'}{TIFF Files}
  \lineii{'rast'}{Sun Raster Files}
  \lineii{'xbm'}{X Bitmap Files}
  \lineii{'jpeg'}{JPEG data in JFIF format}
  \lineii{'bmp'}{BMP files}
  \lineii{'png'}{Portable Network Graphics}
\end{tableii}

You can extend the list of file types \module{imghdr} can recognize by
appending to this variable:

\begin{datadesc}{tests}
A list of functions performing the individual tests.  Each function
takes two arguments: the byte-stream and an open file-like object.
When \function{what()} is called with a byte-stream, the file-like
object will be \code{None}.

The test function should return a string describing the image type if
the test succeeded, or \code{None} if it failed.
\end{datadesc}

Example:

\begin{verbatim}
>>> import imghdr
>>> imghdr.what('/tmp/bass.gif')
'gif'
\end{verbatim}
back to top