<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blue Cog Blog &#187; 101</title>
	<atom:link href="http://www.bluecog.com/blog/tag/101/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bluecog.com/blog</link>
	<description>It's just a freaking blue cog...</description>
	<lastBuildDate>Tue, 03 Aug 2010 19:32:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Python Imaging Library &#8211; Introduction</title>
		<link>http://www.bluecog.com/blog/2009/07/24/python-imaging-library-intro/</link>
		<comments>http://www.bluecog.com/blog/2009/07/24/python-imaging-library-intro/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 20:57:41 +0000</pubDate>
		<dc:creator>Bill Melvin</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[101]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.bluecog.com/blog/?p=245</guid>
		<description><![CDATA[Sometimes you run across an item in an article or blog post that you don&#8217;t take much notice of at the time but it makes just enough of an impression that you recall its existence later, though you may forget the source. I recall reading about working with image files in Python but I don&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes you run across an item in an article or blog post that you don&#8217;t take much notice of at the time but it makes just enough of an impression that you recall its existence later, though you may forget the source. I recall reading about working with image files in <a href="http://www.python.org/">Python</a> but I don&#8217;t remember the source. I do remember there was an example that appeared to be doing some significant image manipulation in just a few lines of code.</p>
<p>It was a few years ago, and some time after that initial encounter, that I found myself with directories full of Windows bitmap files of several megabytes each. These were screen shots captured using either a tool called Screen Seize or using the manual method of pressing Print Screen and pasting into Paint. Regardless of how they got there, it was bugging me that they were taking up so much space. Disks are huge and space is cheap these days but I still recall that the first hard disk drive I used. It had a capacity of 5 MB and cost several thousand dollars. It&#8217;s ingrained that I don&#8217;t like wasting disk space.</p>
<p>Facing that listing of BMP files, the memory of that image manipulation example in Python came back to me. I searched and found the <a href="http://www.pythonware.com/products/pil/index.htm">Python Imaging Library</a> (PIL). You need to have Python installed first. Download and install the version of the PIL to match the installed version of Python and you&#8217;re good to go. The Python installer registers the <strong>.py</strong> extension so typing just the name of a Python script at a command prompt will invoke the Python interpreter to execute the script. I created a script named <strong>bmp2png.py</strong> (the old &#8217;2&#8242; for &#8216;to&#8217;) and placed it in a directory that is in the PATH. To use the script, I simply opened a command prompt in the directory containing the bitmap files and ran bmp2png.py to create a smaller PNG file from each BMP file. Of course I looked at some of the PNG files to make sure the conversion went well before manually deleting the original BMP files. </p>
<p>To anyone familiar with Python, the following is a very obvious and simple script. It may also be non-<em>Pythonic</em>, or wrong in some way. I&#8217;m no Python guru, just a casual enthusiast at this point. There are a few &#8220;extra&#8221; lines in the script. The ones with the <strong>print</strong> statements are just for visual feedback. I like visual feedback (except from other drivers on the freeway).</p>
<pre class="brush: python">
import os, Image

print &#039;Converting BMP to PNG in &#039; + os.getcwd()
ls = os.listdir(os.getcwd())
for f in ls:
    name, ext = os.path.splitext(f)
    if ext.lower() == &quot;.bmp&quot;:
        outfile = name + &quot;.png&quot;
        print &#039;  &#039; + f + &#039; -&gt; &#039; + outfile
        Image.open(f).save(outfile)
print &#039;Done.&#039;
</pre>
<p>Line <strong>1</strong> imports the <strong>os</strong> module needed to work with directories and such, and the <strong>Image</strong> module which contains the Python Imaging Library. Line <strong>4</strong> gets a list of all files in the current working directory, and at line <strong>5</strong> we start working with each file in the list. Line <strong>6</strong> splits the file name and extension into separate variables. We&#8217;ll process only the files with a <strong>.bmp</strong> extension. After making a new file name with the <strong>.png</strong> extension we get to line <strong>10</strong> where the magic happens. The <strong>save</strong> method of the Image object will convert the format of the file based on the extension of the given file name. That&#8217;s all there is to converting the files. Actually there can be a lot more to it if you want. The PIL uses default options when you don&#8217;t specify otherwise, but there are <a href="http://www.pythonware.com/library/pil/handbook/image.htm">options</a> available if you want more control over the conversion. </p>
<p>I have been impressed with what the Python Imaging Library can do, and I&#8217;ve just scratched the surface (oops, better buff that out &#8211; sorry). Though I use more efficient screen capture methods these days, I&#8217;ve found the above script useful from time to time. It was just a starting point. There are several similar, and slightly more advanced, scripts I plan to share in future posts.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bluecog.com/blog/2009/07/24/python-imaging-library-intro/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
