<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>14.6 imghdr --
Determine the type of an image.</TITLE>
<META NAME="description" CONTENT="14.6 imghdr --
Determine the type of an image.">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="lib.css">
<LINK REL="next" HREF="module-sndhdr.html">
<LINK REL="previous" HREF="module-rgbimg.html">
<LINK REL="up" HREF="mmedia.html">
<LINK REL="next" HREF="module-sndhdr.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="module-sndhdr.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="mmedia.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="module-rgbimg.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="module-sndhdr.html">14.7 sndhdr  </A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="mmedia.html">14. Multimedia Services</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="module-rgbimg.html">14.5 rgbimg  </A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION0016600000000000000000">
14.6 <tt class=module>imghdr</tt> --
         Determine the type of an image.</A>
</H1>

<P>

<P>
The <tt class=module>imghdr</tt> module determines the type of image contained in a
file or byte stream.

<P>
The <tt class=module>imghdr</tt> module defines the following function:

<P>
<dl><dt><b><a name="l2h-2284"><tt class=function>what</tt></a></b> (<var>filename</var><big>[</big><var>, h</var><big>]</big>)
<dd>
Tests the image data contained in the file named by <var>filename</var>,
and returns a string describing the image type.  If optional <var>h</var>
is provided, the <var>filename</var> is ignored and <var>h</var> is assumed to
contain the byte stream to test.
</dl>

<P>
The following image types are recognized, as listed below with the
return value from <tt class=function>what()</tt>:

<P>
<table border align=center>
  <thead>
    <tr bgcolor="#99CCFF">
      <th align=left><b>Value</b></th>
      <th align=left><b>Image format</b></th>
    </thead>
  <tbody valign=baseline>
    <tr><td valign=baseline><code>'rgb'</code></td>
        <td>SGI ImgLib Files&nbsp;</td>
    <tr><td valign=baseline><code>'gif'</code></td>
        <td>GIF 87a and 89a Files&nbsp;</td>
    <tr><td valign=baseline><code>'pbm'</code></td>
        <td>Portable Bitmap Files&nbsp;</td>
    <tr><td valign=baseline><code>'pgm'</code></td>
        <td>Portable Graymap Files&nbsp;</td>
    <tr><td valign=baseline><code>'ppm'</code></td>
        <td>Portable Pixmap Files&nbsp;</td>
    <tr><td valign=baseline><code>'tiff'</code></td>
        <td>TIFF Files&nbsp;</td>
    <tr><td valign=baseline><code>'rast'</code></td>
        <td>Sun Raster Files&nbsp;</td>
    <tr><td valign=baseline><code>'xbm'</code></td>
        <td>X Bitmap Files&nbsp;</td>
    <tr><td valign=baseline><code>'jpeg'</code></td>
        <td>JPEG data in JFIF format&nbsp;</td>
    <tr><td valign=baseline><code>'bmp'</code></td>
        <td>BMP files&nbsp;</td>
    <tr><td valign=baseline><code>'png'</code></td>
        <td>Portable Network Graphics&nbsp;</td></tbody>
</table>

<P>
You can extend the list of file types <tt class=module>imghdr</tt> can recognize by
appending to this variable:

<P>
<dl><dt><b><a name="l2h-2285"><tt>tests</tt></a></b>
<dd>
A list of functions performing the individual tests.  Each function
takes two arguments: the byte-stream and an open file-like object.
When <tt class=function>what()</tt> is called with a byte-stream, the file-like
object will be <tt>None</tt>.

<P>
The test function should return a string describing the image type if
the test succeeded, or <tt>None</tt> if it failed.
</dl>

<P>
Example:

<P>
<dl><dd><pre class=verbatim>
&gt;&gt;&gt; import imghdr
&gt;&gt;&gt; imghdr.what('/tmp/bass.gif')
'gif'
</pre></dl><p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="module-sndhdr.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="mmedia.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="module-rgbimg.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="module-sndhdr.html">14.7 sndhdr  </A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="mmedia.html">14. Multimedia Services</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="module-rgbimg.html">14.5 rgbimg  </A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
