<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<link rel=
"STYLESHEET" href=
"lib.css" type='text/css'
/>
<link rel=
"SHORTCUT ICON" href=
"../icons/pyfav.png" type=
"image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index'
/>
<link rel=
"first" href=
"lib.html" title='Python Library Reference'
/>
<link rel='contents' href='contents.html'
title=
"Contents" />
<link rel='index' href='genindex.html' title='Index'
/>
<link rel='last' href='about.html' title='About this document...'
/>
<link rel='help' href='about.html' title='About this document...'
/>
<link rel=
"next" href=
"module-sndhdr.html" />
<link rel=
"prev" href=
"module-rgbimg.html" />
<link rel=
"parent" href=
"mmedia.html" />
<link rel=
"next" href=
"module-sndhdr.html" />
<meta name='aesop' content='information'
/>
<title>14.9 imghdr -- Determine the type of an image
</title>
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"14.8 rgbimg "
href=
"module-rgbimg.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"14. Multimedia Services"
href=
"mmedia.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"14.10 sndhdr "
href=
"module-sndhdr.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"module-rgbimg.html">14.8 rgbimg
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"mmedia.html">14. Multimedia Services
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"module-sndhdr.html">14.10 sndhdr
</A>
<!--End of Navigation Panel-->
<H1><A NAME=
"SECTION0016900000000000000000">
14.9 <tt class=
"module">imghdr
</tt> --
Determine the type of an image
</A>
<A NAME=
"module-imghdr"></A>
The
<tt class=
"module">imghdr
</tt> module determines the type of image contained in a
The
<tt class=
"module">imghdr
</tt> module defines the following function:
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
4762' xml:id='l2h-
4762'
class=
"function">what
</tt></b>(
</nobr></td>
<td><var>filename
</var><big>[
</big><var>, h
</var><big>]
</big><var></var>)
</td></tr></table></dt>
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.
The following image types are recognized, as listed below with the
return value from
<tt class=
"function">what()
</tt>:
<div class=
"center"><table class=
"realtable">
<th class=
"left" >Value
</th>
<th class=
"left" >Image format
</th>
<tr><td class=
"left" valign=
"baseline"><code>'rgb'
</code></td>
<td class=
"left" >SGI ImgLib Files
</td></tr>
<tr><td class=
"left" valign=
"baseline"><code>'gif'
</code></td>
<td class=
"left" >GIF
87a and
89a Files
</td></tr>
<tr><td class=
"left" valign=
"baseline"><code>'pbm'
</code></td>
<td class=
"left" >Portable Bitmap Files
</td></tr>
<tr><td class=
"left" valign=
"baseline"><code>'pgm'
</code></td>
<td class=
"left" >Portable Graymap Files
</td></tr>
<tr><td class=
"left" valign=
"baseline"><code>'ppm'
</code></td>
<td class=
"left" >Portable Pixmap Files
</td></tr>
<tr><td class=
"left" valign=
"baseline"><code>'tiff'
</code></td>
<td class=
"left" >TIFF Files
</td></tr>
<tr><td class=
"left" valign=
"baseline"><code>'rast'
</code></td>
<td class=
"left" >Sun Raster Files
</td></tr>
<tr><td class=
"left" valign=
"baseline"><code>'xbm'
</code></td>
<td class=
"left" >X Bitmap Files
</td></tr>
<tr><td class=
"left" valign=
"baseline"><code>'jpeg'
</code></td>
<td class=
"left" >JPEG data in JFIF format
</td></tr>
<tr><td class=
"left" valign=
"baseline"><code>'bmp'
</code></td>
<td class=
"left" >BMP files
</td></tr>
<tr><td class=
"left" valign=
"baseline"><code>'png'
</code></td>
<td class=
"left" >Portable Network Graphics
</td></tr></tbody>
You can extend the list of file types
<tt class=
"module">imghdr
</tt> can recognize by
appending to this variable:
<dl><dt><b><tt id='l2h-
4763' xml:id='l2h-
4763'
>tests
</tt></b></dt>
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
<code>None
</code>.
The test function should return a string describing the image type if
the test succeeded, or
<code>None
</code> if it failed.
<div class=
"verbatim"><pre>
>>> import imghdr
>>> imghdr.what('/tmp/bass.gif')
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"14.8 rgbimg "
href=
"module-rgbimg.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"14. Multimedia Services"
href=
"mmedia.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"14.10 sndhdr "
href=
"module-sndhdr.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"module-rgbimg.html">14.8 rgbimg
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"mmedia.html">14. Multimedia Services
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"module-sndhdr.html">14.10 sndhdr
</A>
<span class=
"release-info">Release
2.4.2, documentation updated on
28 September
2005.
</span>
<!--End of Navigation Panel-->
See
<i><a href=
"about.html">About this document...
</a></i> for information on suggesting changes.