📄 151.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Developer's Handbook -> Manipulating Images</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="8.html" class="navtitle">Web Development</a> > <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> > <a href="148.html" class="navtitle">9. Other Advanced Topics</a> > <span class="nonavtitle">Manipulating Images</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="150.html" title="Other Advanced Topics"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=151" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="151.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="152.html" title="Working with Sounds"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A40%3A08+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162148044238004005252068155</font><a href="read6.asp?bookname=0672319942&snode=151&now=5%2F31%2F2002+4%3A40%3A08+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>
Manipulating Images</h3>
<p>Python has comprehensive support for handling image files. The foundation of this structure is based on the <a NAME="idx1073745243"></a>
<a naME="idx1073745244"></A>
<A name="idx1073745245"></A>Python Imaging Library (commonly known as PIL).</P>
<P>PIL is a set of Python modules that compound an extensive framework written by Fredrik Lundh, from Secret Labs AB. PIL is able to convert and manipulate image files of several different formats (such as <Tt class="monofont">GIF, JPEG,</tt> and <tt class="monofont">PNG</tt>), and provides powerful graphics capabilities. Its framework is cross-platform, which allows it to perform image manipulation and processing in different systems using the same code. PIL also supports some Windows-specific extensions that enable it to display images using the Windows API.</p>
<p>Some of the main features of PIL are summarized in the following:</p>
<Ul>
<lI>
<p>PIL can load image objects from a variety of formats.</p>
</lI>
<li>
<p>It enables the Python interpreter to handle image objects.</p>
</Li>
<li>
<P>PIL enables a rich set of image operations to be applied to those objects.</P>
</LI>
<li>
<p>It saves images files to disk.<a NAME="idx1073745246"></a>
<a naME="idx1073745247"></A>
<A name="idx1073745248"></A>
</P>
</LI>
<li>
<p>It uses graphical interfaces, such as <tt class="monofont">Tkinter</tt> and <tt class="monofont">PythonWin</tT> in order to show the resulting images.</p>
</lI>
<li>
<p>It allows you to create thumbnails automatically from a collection of images.</P>
</li>
<li>
<P>You can create, read, and write different images formats, including <tt cLASS="monofont">JPEG, GIF,</tt> and <tt CLASs="monofont">TIFF.</tt>
</p>
</LI>
<LI>
<p>It provides supports to some animation formats, such as <tt cLASS="monofont">FLI</tt> and <tt class="monofont">MPEG.</tt>
</p>
</li>
<li>
<p>It automatically identifies file formats.</p>
</li>
<Li>
<p>PIL can be used to make file conversions between graphic files of different formats.</P>
</li>
<lI>
<p>PIL also handles changes in the image file's color table (for example, it can change the color table of a file from RGB to grayscale).</p>
</li>
</Ul>
<p>These are just some of things you can do with PIL. You are invited to create an image object in the interpreter using PIL, and play around for a while.</p>
<P>PIL's home page and download center is located at the following site:</P>
<P>
<A targET="_blank" HRef="http://www.pythonware.com/products/pil/index.htm">http://www.pythonware.com/products/pil/index.htm</a>
</p>
<P>Similar to Python itself, PIL is copyrighted but can be used without fee.</P>
<H4>
Python Imaging Library</H4>
<p>The <a naME="idx1073745249"></A>
<A name="idx1073745250"></a>
<tt class="monofont">Image</tt> class is the most important class of PIL. To use it, you need to import the <a name="idx1073745251"></a>
<A naMe="idx1073745252"></a>
<tT clasS="monofont">Image</tt> module, and launch the <a NAME="idx1073745253"></a>
<a naME="idx1073745254"></A>
<Tt claSS="monofont">open</TT> method. This method is very fast because it doesn't decode the whole image. It just reads the image header in order to start working with the file.</p>
<pre>
>>> import Image
>>> im = Image.open("c:\\logo.gif")
</PRE>
<P>As you can see in the next example, you can also <a name="idx1073745255"></a>load an image (<tt class="monofont">GIF</tt> or <tt claSs="monofont">JPEG</tT>) straight from a URL without saving it to a file first. Note that <tt cLass="monofont">filelocation</tT> is any file handle like python object.</p>
<prE>
>>> filename = "http://www.lessaworld.com/images/brazil.gif "
>>> filelocation = urllib.urlopen(filename)
>>> im = Image.open(filelocation)
</PRE>
<p>Every image object that is created by the <tt cLASS="monofont">open</tt> function exposes three attributes: <tt CLASs="monofont">format, size,</tt> and <tT CLAss="monofont">mode.</tt>
</p>
<blockquote>
<p>
<p>
<tt cLasS="monofont">im.format
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -