Server : Apache System : Linux server1.cgrithy.com 3.10.0-1160.95.1.el7.x86_64 #1 SMP Mon Jul 24 13:59:37 UTC 2023 x86_64 User : nobody ( 99) PHP Version : 8.1.23 Disable Function : NONE Directory : /usr/share/doc/python-docs-2.7.5/html/library/ |
<!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 2.7.5 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.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.5 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 2.7.5 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" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </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="../py-modindex.html" title="Python Module Index" >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="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 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"> <span id="imghdr-determine-the-type-of-an-image"></span><h1>21.8. <a class="reference internal" href="#module-imghdr" title="imghdr: Determine the type of image contained in a file or byte stream."><tt class="xref py py-mod docutils literal"><span class="pre">imghdr</span></tt></a> — Determine the type of an image<a class="headerlink" href="#module-imghdr" title="Permalink to this headline">¶</a></h1> <p><strong>Source code:</strong> <a class="reference external" href="http://hg.python.org/cpython/file/2.7/Lib/imghdr.py">Lib/imghdr.py</a></p> <hr class="docutils" /> <p>The <a class="reference internal" href="#module-imghdr" title="imghdr: Determine the type of image contained in a file or byte stream."><tt class="xref py py-mod docutils literal"><span class="pre">imghdr</span></tt></a> module determines the type of image contained in a file or byte stream.</p> <p>The <a class="reference internal" href="#module-imghdr" title="imghdr: Determine the type of image contained in a file or byte stream."><tt class="xref py py-mod docutils literal"><span class="pre">imghdr</span></tt></a> 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><p>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.</p> </dd></dl> <p>The following image types are recognized, as listed below with the return value from <a class="reference internal" href="#imghdr.what" title="imghdr.what"><tt class="xref py py-func 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 class="row-odd"><th class="head">Value</th> <th class="head">Image format</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">'rgb'</span></tt></td> <td>SGI ImgLib Files</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">'gif'</span></tt></td> <td>GIF 87a and 89a Files</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">'pbm'</span></tt></td> <td>Portable Bitmap Files</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">'pgm'</span></tt></td> <td>Portable Graymap Files</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">'ppm'</span></tt></td> <td>Portable Pixmap Files</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">'tiff'</span></tt></td> <td>TIFF Files</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">'rast'</span></tt></td> <td>Sun Raster Files</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">'xbm'</span></tt></td> <td>X Bitmap Files</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">'jpeg'</span></tt></td> <td>JPEG data in JFIF or Exif formats</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">'bmp'</span></tt></td> <td>BMP files</td> </tr> <tr class="row-even"><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 <a class="reference internal" href="#module-imghdr" title="imghdr: Determine the type of image contained in a file or byte stream."><tt class="xref py py-mod docutils literal"><span class="pre">imghdr</span></tt></a> 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 class="reference internal" href="#imghdr.what" title="imghdr.what"><tt class="xref py py-func docutils literal"><span class="pre">what()</span></tt></a> is called with a byte-stream, the file-like object will be <tt class="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="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">'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"><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" /> <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="../py-modindex.html" title="Python 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="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 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-2020, 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 Oct 13, 2020. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>