<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>21.8. imghdr — Determine the type of an image — Python v2.6.6 documentation</title>
<link rel="stylesheet" href="../_static/default.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '2.6.6',
COLLAPSE_MODINDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="Search within Python v2.6.6 documentation"
href="../_static/opensearch.xml"/>
<link rel="author" title="About these documents" href="../about.html" />
<link rel="copyright" title="Copyright" href="../copyright.html" />
<link rel="top" title="Python v2.6.6 documentation" href="../index.html" />
<link rel="up" title="21. Multimedia Services" href="mm.html" />
<link rel="next" title="21.9. sndhdr — Determine type of sound file" href="sndhdr.html" />
<link rel="prev" title="21.7. colorsys — Conversions between color systems" href="colorsys.html" />
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../modindex.html" title="Global Module Index"
accesskey="M">modules</a> |</li>
<li class="right" >
<a href="sndhdr.html" title="21.9. sndhdr — Determine type of sound file"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="colorsys.html" title="21.7. colorsys — Conversions between color systems"
accesskey="P">previous</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="../index.html">Python v2.6.6 documentation</a> »</li>
<li><a href="index.html" >The Python Standard Library</a> »</li>
<li><a href="mm.html" accesskey="U">21. Multimedia Services</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="module-imghdr">
<h1>21.8. <tt class="xref docutils literal"><span class="pre">imghdr</span></tt> — Determine the type of an image<a class="headerlink" href="#module-imghdr" title="Permalink to this headline">¶</a></h1>
<p>The <tt class="xref docutils literal"><span class="pre">imghdr</span></tt> module determines the type of image contained in a file or
byte stream.</p>
<p>The <tt class="xref docutils literal"><span class="pre">imghdr</span></tt> module defines the following function:</p>
<dl class="function">
<dt id="imghdr.what">
<tt class="descclassname">imghdr.</tt><tt class="descname">what</tt><big>(</big><em>filename</em><span class="optional">[</span>, <em>h</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#imghdr.what" title="Permalink to this definition">¶</a></dt>
<dd>Tests the image data contained in the file named by <em>filename</em>, and returns a
string describing the image type. If optional <em>h</em> is provided, the <em>filename</em>
is ignored and <em>h</em> is assumed to contain the byte stream to test.</dd></dl>
<p>The following image types are recognized, as listed below with the return value
from <a title="imghdr.what" class="reference internal" href="#imghdr.what"><tt class="xref docutils literal"><span class="pre">what()</span></tt></a>:</p>
<table border="1" class="docutils">
<colgroup>
<col width="26%" />
<col width="74%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Value</th>
<th class="head">Image format</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><tt class="docutils literal"><span class="pre">'rgb'</span></tt></td>
<td>SGI ImgLib Files</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'gif'</span></tt></td>
<td>GIF 87a and 89a Files</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'pbm'</span></tt></td>
<td>Portable Bitmap Files</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'pgm'</span></tt></td>
<td>Portable Graymap Files</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'ppm'</span></tt></td>
<td>Portable Pixmap Files</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'tiff'</span></tt></td>
<td>TIFF Files</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'rast'</span></tt></td>
<td>Sun Raster Files</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'xbm'</span></tt></td>
<td>X Bitmap Files</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'jpeg'</span></tt></td>
<td>JPEG data in JFIF or Exif formats</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'bmp'</span></tt></td>
<td>BMP files</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'png'</span></tt></td>
<td>Portable Network Graphics</td>
</tr>
</tbody>
</table>
<p class="versionadded">
<span class="versionmodified">New in version 2.5: </span>Exif detection.</p>
<p>You can extend the list of file types <tt class="xref docutils literal"><span class="pre">imghdr</span></tt> can recognize by appending
to this variable:</p>
<dl class="data">
<dt id="imghdr.tests">
<tt class="descclassname">imghdr.</tt><tt class="descname">tests</tt><a class="headerlink" href="#imghdr.tests" title="Permalink to this definition">¶</a></dt>
<dd><p>A list of functions performing the individual tests. Each function takes two
arguments: the byte-stream and an open file-like object. When <a title="imghdr.what" class="reference internal" href="#imghdr.what"><tt class="xref docutils literal"><span class="pre">what()</span></tt></a> is
called with a byte-stream, the file-like object will be <tt class="xref docutils literal"><span class="pre">None</span></tt>.</p>
<p>The test function should return a string describing the image type if the test
succeeded, or <tt class="xref docutils literal"><span class="pre">None</span></tt> if it failed.</p>
</dd></dl>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">imghdr</span>
<span class="gp">>>> </span><span class="n">imghdr</span><span class="o">.</span><span class="n">what</span><span class="p">(</span><span class="s">'/tmp/bass.gif'</span><span class="p">)</span>
<span class="go">'gif'</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h4>Previous topic</h4>
<p class="topless"><a href="colorsys.html"
title="previous chapter">21.7. <tt class="docutils literal docutils literal docutils literal"><span class="pre">colorsys</span></tt> — Conversions between color systems</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="sndhdr.html"
title="next chapter">21.9. <tt class="docutils literal"><span class="pre">sndhdr</span></tt> — Determine type of sound file</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">Report a Bug</a></li>
<li><a href="../_sources/library/imghdr.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="../search.html" method="get">
<input type="text" name="q" size="18" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../modindex.html" title="Global Module Index"
>modules</a> |</li>
<li class="right" >
<a href="sndhdr.html" title="21.9. sndhdr — Determine type of sound file"
>next</a> |</li>
<li class="right" >
<a href="colorsys.html" title="21.7. colorsys — Conversions between color systems"
>previous</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="../index.html">Python v2.6.6 documentation</a> »</li>
<li><a href="index.html" >The Python Standard Library</a> »</li>
<li><a href="mm.html" >21. Multimedia Services</a> »</li>
</ul>
</div>
<div class="footer">
© <a href="../copyright.html">Copyright</a> 1990-2011, Python Software Foundation.
<br />
The Python Software Foundation is a non-profit corporation.
<a href="http://www.python.org/psf/donations/">Please donate.</a>
<br />
Last updated on Jul 20, 2011.
<a href="../bugs.html">Found a bug</a>?
<br />
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.6.
</div>
</body>
</html>
Copyright 2K16 - 2K18 Indonesian Hacker Rulez