1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>21.8. imghdr — Determine the type of an image — Python 2.7.11 documentation</title>
11
<link rel="stylesheet" href="../_static/classic.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 2.7.11 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 2.7.11 documentation" href="../contents.html" />
33
<link rel="up" title="21. Multimedia Services" href="mm.html" />
34
<link rel="next" title="21.9. sndhdr — Determine type of sound file" href="sndhdr.html" />
35
<link rel="prev" title="21.7. colorsys — Conversions between color systems" href="colorsys.html" />
36
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
<script type="text/javascript" src="../_static/copybutton.js"></script>
38
<script type="text/javascript" src="../_static/version_switch.js"></script>
43
<body role="document">
44
<div class="related" role="navigation" aria-label="related navigation">
47
<li class="right" style="margin-right: 10px">
48
<a href="../genindex.html" title="General Index"
49
accesskey="I">index</a></li>
51
<a href="../py-modindex.html" title="Python Module Index"
54
<a href="sndhdr.html" title="21.9. sndhdr — Determine type of sound file"
55
accesskey="N">next</a> |</li>
57
<a href="colorsys.html" title="21.7. colorsys — Conversions between color systems"
58
accesskey="P">previous</a> |</li>
59
<li><img src="../_static/py.png" alt=""
60
style="vertical-align: middle; margin-top: -1px"/></li>
61
<li><a href="https://www.python.org/">Python</a> »</li>
63
<span class="version_switcher_placeholder">2.7.11</span>
64
<a href="../index.html">Documentation</a> »
67
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
68
<li class="nav-item nav-item-2"><a href="mm.html" accesskey="U">21. Multimedia Services</a> »</li>
72
<div class="document">
73
<div class="documentwrapper">
74
<div class="bodywrapper">
75
<div class="body" role="main">
77
<div class="section" id="module-imghdr">
78
<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."><code class="xref py py-mod docutils literal"><span class="pre">imghdr</span></code></a> — Determine the type of an image<a class="headerlink" href="#module-imghdr" title="Permalink to this headline">¶</a></h1>
79
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/2.7/Lib/imghdr.py">Lib/imghdr.py</a></p>
80
<hr class="docutils" />
81
<p>The <a class="reference internal" href="#module-imghdr" title="imghdr: Determine the type of image contained in a file or byte stream."><code class="xref py py-mod docutils literal"><span class="pre">imghdr</span></code></a> module determines the type of image contained in a file or
83
<p>The <a class="reference internal" href="#module-imghdr" title="imghdr: Determine the type of image contained in a file or byte stream."><code class="xref py py-mod docutils literal"><span class="pre">imghdr</span></code></a> module defines the following function:</p>
86
<code class="descclassname">imghdr.</code><code class="descname">what</code><span class="sig-paren">(</span><em>filename</em><span class="optional">[</span>, <em>h</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#imghdr.what" title="Permalink to this definition">¶</a></dt>
87
<dd><p>Tests the image data contained in the file named by <em>filename</em>, and returns a
88
string describing the image type. If optional <em>h</em> is provided, the <em>filename</em>
89
is ignored and <em>h</em> is assumed to contain the byte stream to test.</p>
92
<p>The following image types are recognized, as listed below with the return value
93
from <a class="reference internal" href="#imghdr.what" title="imghdr.what"><code class="xref py py-func docutils literal"><span class="pre">what()</span></code></a>:</p>
94
<table border="1" class="docutils">
99
<thead valign="bottom">
100
<tr class="row-odd"><th class="head">Value</th>
101
<th class="head">Image format</th>
105
<tr class="row-even"><td><code class="docutils literal"><span class="pre">'rgb'</span></code></td>
106
<td>SGI ImgLib Files</td>
108
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">'gif'</span></code></td>
109
<td>GIF 87a and 89a Files</td>
111
<tr class="row-even"><td><code class="docutils literal"><span class="pre">'pbm'</span></code></td>
112
<td>Portable Bitmap Files</td>
114
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">'pgm'</span></code></td>
115
<td>Portable Graymap Files</td>
117
<tr class="row-even"><td><code class="docutils literal"><span class="pre">'ppm'</span></code></td>
118
<td>Portable Pixmap Files</td>
120
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">'tiff'</span></code></td>
123
<tr class="row-even"><td><code class="docutils literal"><span class="pre">'rast'</span></code></td>
124
<td>Sun Raster Files</td>
126
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">'xbm'</span></code></td>
127
<td>X Bitmap Files</td>
129
<tr class="row-even"><td><code class="docutils literal"><span class="pre">'jpeg'</span></code></td>
130
<td>JPEG data in JFIF or Exif formats</td>
132
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">'bmp'</span></code></td>
135
<tr class="row-even"><td><code class="docutils literal"><span class="pre">'png'</span></code></td>
136
<td>Portable Network Graphics</td>
140
<div class="versionadded">
141
<p><span class="versionmodified">New in version 2.5: </span>Exif detection.</p>
143
<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."><code class="xref py py-mod docutils literal"><span class="pre">imghdr</span></code></a> can recognize by appending
144
to this variable:</p>
146
<dt id="imghdr.tests">
147
<code class="descclassname">imghdr.</code><code class="descname">tests</code><a class="headerlink" href="#imghdr.tests" title="Permalink to this definition">¶</a></dt>
148
<dd><p>A list of functions performing the individual tests. Each function takes two
149
arguments: the byte-stream and an open file-like object. When <a class="reference internal" href="#imghdr.what" title="imghdr.what"><code class="xref py py-func docutils literal"><span class="pre">what()</span></code></a> is
150
called with a byte-stream, the file-like object will be <code class="docutils literal"><span class="pre">None</span></code>.</p>
151
<p>The test function should return a string describing the image type if the test
152
succeeded, or <code class="docutils literal"><span class="pre">None</span></code> if it failed.</p>
156
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">imghdr</span>
157
<span class="gp">>>> </span><span class="n">imghdr</span><span class="o">.</span><span class="n">what</span><span class="p">(</span><span class="s1">'bass.gif'</span><span class="p">)</span>
158
<span class="go">'gif'</span>
167
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
168
<div class="sphinxsidebarwrapper">
169
<h4>Previous topic</h4>
170
<p class="topless"><a href="colorsys.html"
171
title="previous chapter">21.7. <code class="docutils literal"><span class="pre">colorsys</span></code> — Conversions between color systems</a></p>
173
<p class="topless"><a href="sndhdr.html"
174
title="next chapter">21.9. <code class="docutils literal"><span class="pre">sndhdr</span></code> — Determine type of sound file</a></p>
176
<ul class="this-page-menu">
177
<li><a href="../bugs.html">Report a Bug</a></li>
178
<li><a href="../_sources/library/imghdr.txt"
179
rel="nofollow">Show Source</a></li>
182
<div id="searchbox" style="display: none" role="search">
183
<h3>Quick search</h3>
184
<form class="search" action="../search.html" method="get">
185
<input type="text" name="q" />
186
<input type="submit" value="Go" />
187
<input type="hidden" name="check_keywords" value="yes" />
188
<input type="hidden" name="area" value="default" />
190
<p class="searchtip" style="font-size: 90%">
191
Enter search terms or a module, class or function name.
194
<script type="text/javascript">$('#searchbox').show(0);</script>
197
<div class="clearer"></div>
199
<div class="related" role="navigation" aria-label="related navigation">
202
<li class="right" style="margin-right: 10px">
203
<a href="../genindex.html" title="General Index"
206
<a href="../py-modindex.html" title="Python Module Index"
209
<a href="sndhdr.html" title="21.9. sndhdr — Determine type of sound file"
212
<a href="colorsys.html" title="21.7. colorsys — Conversions between color systems"
214
<li><img src="../_static/py.png" alt=""
215
style="vertical-align: middle; margin-top: -1px"/></li>
216
<li><a href="https://www.python.org/">Python</a> »</li>
218
<span class="version_switcher_placeholder">2.7.11</span>
219
<a href="../index.html">Documentation</a> »
222
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
223
<li class="nav-item nav-item-2"><a href="mm.html" >21. Multimedia Services</a> »</li>
227
© <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
229
The Python Software Foundation is a non-profit corporation.
230
<a href="https://www.python.org/psf/donations/">Please donate.</a>
232
Last updated on Jan 23, 2016.
233
<a href="../bugs.html">Found a bug</a>?
235
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'