<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet href="http://www.pythonblogs.com/styles/rss.css" type="text/css"?>
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
>
 <channel>
  <title>Python Examples</title>
  <link>http://python-examples.pythonblogs.com/31_python_examples</link>
  <description>&lt;p&gt;Ctrl+C...Ctrl+V&lt;br /&gt;
Copy and paste create your code&lt;/p&gt;
</description>
  <pubDate>Wed, 28 Jul 2010 03:38:45 -0700</pubDate>
  <generator>http://www.lifetype.net</generator>
    <item>
   <title>Python - Resize Images with PIL</title>
   <description>
    &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;http://python-examples.pythonblogs.com/plugins/plogeshi/styles/plogeshi.css&quot; /&gt;&lt;p class=&quot;MsoNormal&quot;&gt;&lt;span&gt;Here is a function, which can be called somewhere to resize images. It works resizing the image to user defined width and height, or setting width (or height) to -1, resize the new dimensions maintaining the same aspect ratio.&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot;&gt;&lt;span&gt;It is possible to set even the quality of the image.&lt;/span&gt;&lt;/p&gt;&lt;p class=&quot;MsoNormal&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class=&quot;MsoNormal&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;color: #1f497d&quot;&gt; &lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 1cm&quot;&gt;&lt;span style=&quot;color: #1f497d&quot;&gt;def&lt;/span&gt;&lt;span&gt; Resize(file,width,height,quality=80):&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 42.55pt&quot;&gt;&lt;span&gt;path,ext=os.path.splitext(file)&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 42.55pt&quot;&gt;&lt;span&gt;name=os.path.basename(path)&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 42.55pt&quot;&gt;&lt;span style=&quot;color: #1f497d&quot;&gt;try:&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 2cm&quot;&gt;&lt;span&gt;image=Image.open(file)&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 42.55pt&quot;&gt;&lt;span style=&quot;color: #1f497d&quot;&gt;except:&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 2cm&quot;&gt;&lt;span style=&quot;color: #1f497d&quot;&gt;print&lt;/span&gt;&lt;span&gt; &amp;quot;Error! -&amp;gt; file:&amp;quot;,name+ext&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 2cm&quot;&gt;&lt;span style=&quot;color: #1f497d&quot;&gt;return&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 42.55pt&quot;&gt;&lt;span&gt;w,h=image.size&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 42.55pt&quot;&gt;&lt;span style=&quot;color: #1f497d&quot;&gt;if&lt;/span&gt;&lt;span&gt; width==-1 &lt;span style=&quot;color: #1f497d&quot;&gt;and&lt;/span&gt; height!=-1:&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 2cm&quot;&gt;&lt;span&gt;w=height*w/h&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 2cm&quot;&gt;&lt;span&gt;h=height&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 42.55pt&quot;&gt;&lt;span style=&quot;color: #1f497d&quot;&gt;if&lt;/span&gt;&lt;span&gt; height==-1 &lt;span style=&quot;color: #1f497d&quot;&gt;and&lt;/span&gt; width!=-1:&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 2cm&quot;&gt;&lt;span&gt;h=width*h/w&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 2cm&quot;&gt;&lt;span&gt;w=width&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 42.55pt&quot;&gt;&lt;span style=&quot;color: #1f497d&quot;&gt;if&lt;/span&gt;&lt;span&gt; width!=-1 &lt;span style=&quot;color: #1f497d&quot;&gt;and&lt;/span&gt; height!=-1:&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 2cm&quot;&gt;&lt;span&gt;w=width&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 2cm&quot;&gt;&lt;span&gt;h=height&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 42.55pt&quot;&gt;&lt;span&gt;fmt=image.format&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 42.55pt&quot;&gt;&lt;span style=&quot;color: #1f497d&quot;&gt;try:&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 2cm&quot;&gt;&lt;span&gt;image=image.resize((w,h),Image.ANTIALIAS).save(file,fmt,quality=quality)&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 42.55pt&quot;&gt;&lt;span style=&quot;color: #1f497d&quot;&gt;except:&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 2cm&quot;&gt;&lt;span style=&quot;color: #1f497d&quot;&gt;print&lt;/span&gt;&lt;span&gt; &amp;quot;Error! -&amp;gt; file:&amp;quot;,name+ext&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 2cm&quot;&gt;&lt;span style=&quot;color: #1f497d&quot;&gt;return&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 42.55pt&quot;&gt;&lt;span style=&quot;color: #1f497d&quot;&gt;print&lt;/span&gt;&lt;span&gt; name+ext,&amp;quot;...Ok!&amp;quot;&lt;/span&gt;&lt;/p&gt;&lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 42.55pt&quot;&gt;&amp;nbsp;&lt;/p&gt;&lt;p class=&quot;MsoNormal&quot; style=&quot;text-indent: 42.55pt&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot;&gt;&lt;span&gt; &lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot;&gt;&lt;span&gt;A more complex code is&lt;/span&gt;&lt;span&gt; &lt;a href=&quot;http://www.webalice.it/cronos80/imgGUI.py&quot;&gt;here&lt;/a&gt;&lt;/span&gt;&lt;span&gt;, it&amp;rsquo;s a script that use wx as a graphical user interface.&lt;/span&gt;&lt;/p&gt;  &lt;p class=&quot;MsoNormal&quot;&gt;&lt;span&gt; &lt;/span&gt;&lt;/p&gt;
   </description>
   <link>http://python-examples.pythonblogs.com/31_python_examples/archive/65_python-resize_images_with_pil.html</link>
   <comments>http://python-examples.pythonblogs.com/31_python_examples/archive/65_python-resize_images_with_pil.html</comments>
   <guid>http://python-examples.pythonblogs.com/31_python_examples/archive/65_python-resize_images_with_pil.html</guid>
      <dc:creator>giuamato</dc:creator>
      
    <category>*</category>
      
    <category>Images</category>
         <pubDate>Mon, 27 Apr 2009 13:49:00 +0200</pubDate>
   <source url="http://python-examples.pythonblogs.com/31_python_examples/feeds/rss20">Python Examples</source>
     </item>
   </channel>
</rss>