Python Imaging Library – Introduction

Sometimes you run across an item in an article or blog post that you don’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’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.

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’s ingrained that I don’t like wasting disk space.

Facing that listing of BMP files, the memory of that image manipulation example in Python came back to me. I searched and found the Python Imaging Library (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’re good to go. The Python installer registers the .py 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 bmp2png.py (the old ‘2’ for ‘to’) 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.

To anyone familiar with Python, the following is a very obvious and simple script. It may also be non-Pythonic, or wrong in some way. I’m no Python guru, just a casual enthusiast at this point. There are a few “extra” lines in the script. The ones with the print statements are just for visual feedback. I like visual feedback (except from other drivers on the freeway).

import os, Image

print 'Converting BMP to PNG in ' + os.getcwd()
ls = os.listdir(os.getcwd())
for f in ls:
    name, ext = os.path.splitext(f)
    if ext.lower() == ".bmp":
        outfile = name + ".png"
        print '  ' + f + ' -> ' + outfile
        Image.open(f).save(outfile)
print 'Done.'

Line 1 imports the os module needed to work with directories and such, and the Image module which contains the Python Imaging Library. Line 4 gets a list of all files in the current working directory, and at line 5 we start working with each file in the list. Line 6 splits the file name and extension into separate variables. We’ll process only the files with a .bmp extension. After making a new file name with the .png extension we get to line 10 where the magic happens. The save method of the Image object will convert the format of the file based on the extension of the given file name. That’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’t specify otherwise, but there are options available if you want more control over the conversion.

I have been impressed with what the Python Imaging Library can do, and I’ve just scratched the surface (oops, better buff that out – sorry). Though I use more efficient screen capture methods these days, I’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.

CONDG Meeting – July 2009

Wow! I have been remiss as a blogger. No posts since April. I logged in and see there are five drafts I haven’t finished. I don’t know if this has anything to do with the fact that I started using Twitter in the meantime. Twitter: It’s like a sputtering of creative sparks, 140 character sparks at most (and mine not all that creative), that burn through the fuel of creative energy but never really get the fire going. There is something addictive about Twitter when you’re a geek. Maybe I shouldn’t blame my lack of writing on Twitter. There have been a lot of other things going on the last few months. On the bright side, I doubt many read this blog (if I checked metrics I’d know) so it’s not a big deal. But even if this is only a journal for my own future reference I should keep it up, right? Well, on to the meeting.

At this month’s meeting of the Central Ohio .NET Developers Group, Jeremiah Peschka (already following Jeremiah on Twitter) talked about SQL Server and Object-Relational Mapping. Jeremiah talked specifically about the NHibernate ORM tool. I’ve read a lot about NHibernate but so far have not worked on a project that used it. Prior to showing NHibernate, the support for hierarchical data in SQL Server was discussed. It seems that this hierarchical data could be useful in ORM scenarios. I really enjoyed the presentation and look forward to working with some of the tools and techniques that were discussed.

On a side note: Maybe it’s just me, but there’s something about Jeremiah’s mannerisms that reminds me of Clark Howard (just followed Clark Howard on Twitter). Of course Jeremiah is a much cooler guy than Clark, maybe not as rich. Of course I say that without really knowing either of them. And maybe I should be tweeting this instead.

COLARC Meeting – April 2009

Software Archeology was the topic of the presentation given by Michael Rozlog of Embarcadero Technologies at this month’s meeting of the Columbus Architecture Group. As a software developer, you sometimes inherit code that you had nothing to do with creating. This can happen for a variety of reasons but the end result is you have a big lump of code that you know little to nothing about. As Michael described it, that code is effectively locked until you take steps to discover how it works. Michael described following aspects of “unlocking” an unfamiliar codebase using software tools and showed some examples using JBuilder:

  • Visualization using a modeling tool that can map existing code.
  • Using a metrics tool to find design violations.
  • Using an auditing tool to find style violations.
  • Reviewing business logic (unit tests, acceptance tests – if you’re lucky enough to have inherited those too).
  • Using a profiling tool to find areas in the code with performance problems.
  • Creating documentation so future maintainers will not have to start from scratch.

From my experience, sometimes you don’t have modeling, auditing, or metrics tools available for a particular system. You just have to grab a shovel and start digging up bones. Make a copy of the code and do some throw-away refactoring (that code will never be checked back into source control). Try to break parts of the system and see if it breaks in the way you expect. Try to come up with some big picture tests that can be automated. Later, when you know more about the system and are ready to start fixing bugs and adding features, you can add unit tests around just the parts you are going to change. Though some environments just don’t support unit tests, but I digress (as usual).

I have a small company background and at this point I’ve never worked with a system approaching a million lines of code. I’m sure there are systems too large to explore manually that can only be discovered using a tool suite that’s up to the task. Nonetheless, I am reminded by Michael’s examples at this meeting, if you have the tools available you should use them, even for a small system, even for your own code that you think you know well. You’re bound to learn something, and make your code better in the process.

Also in my notes from the meeting:

Kivat graphs

Shotgun Surgery

Stupid iPhone apps can make a lot of money if they go viral.

…and from Microsoft, who provided the meeting space:

Are you certifiable?

IT Management Community Hub

CONDG Meeting – March 2009

I knew this month’s meeting of the Central Ohio .NET Developers Group was going to be a break from the norm when I heard Alan Stevens jamming on his guitar as I approached the meeting room. He was belting out the words to REM’s End of the World as we Know It (and I feel fine) which is an impressive feat of memory and diction in itself.

Alan titled the talk “Coding in Public” and challenged those in attendance to code with others more often. We should not be afraid our code sucks but rather acknowledge that it sucks (at least some of it, sometimes) and, since it sucks anyway, we might as well let it “suck with gusto.” We can then learn together how to make it suck less.

Alan began by discussing the Dreyfus model of skill acquisition in general and as it relates to software development. This was not a new concept for me, but only because I have been reading Pragmatic Thinking and Learning by Andy Hunt. Andy talks a lot about the Dreyfus model. He also mentions something called “second-order incompetence” that worries me a little. Hope that shoe doesn’t fit.

grin

Alan also talked about the book Mastery by George Leonard. When I got home I pulled my copy off the shelf and plan to read it again. It is a good book and the ideas are worth refreshing.

Mastery by George Leonard

Among other books Alan mentioned are Working Effectively with Legacy Code by Michael Feathers, Good to Great by Jim Collins, and Talent Is Overrated by Geoff Colvin.

This talk got me thinking about the relative isolation I’ve been working in lately and that I need to get out more and look for opportunities to collaborate with others. In the meantime, I’ve been wanting to put some code out there for a couple small personal projects I’ve worked on recently and I hope to do that soon. It can’t suck with gusto if it’s stuck in my private Subversion repository. I just have to decide where to host it and which license to use.

Also noted at the meeting:

Code Kata

LiveMesh

Central Ohio Day Of DotNET 2009-04-18

Stir Trek 2009-05-08

CodeStock

The Making of an Expert – This link is not to the whole article, you have to subscribe or buy it (and there’s nothing wrong with wanting to get paid, but then I assume you know how to use a search engine – seek and ye shall find – the choice is yours).

ColARC Meeting – March 2009

I’ve heard the announcements for the meetings of the Columbus Architecture Group few times at the CONDG meetings and decided to check it out. They did a series of short presentations in the lightning talk (also known as blitz babble) format.

Brian Disbrow talked about company strategy as it relates to the tensions and trade-offs between achieving operational excellence, customer alliance (that’s not the term he used, I should take better notes), and market leader position.

Andrew Holowaty, who works for Agile Technologies, talked about a custom reporting framework he has been working on. He uses ASP.NET and the Enterprise Library to deliver customizable reports in a lightweight framework that is configured using angle brackets (ok, there’s some stuff between the brackets too).

James Bender was riding a bus in the clouds after spending too much time at the Mellow Mushroom.

Craig Stuntz talked about the ubiquitous URI and some subtleties surrounding its use in web-based systems. He gave some good food for thought regarding the benefit of immutable URIs that convey meaning without revealing much about the underlying system. If a URL (a form of URI) ends with .php or .aspx, for example, there is a greater chance that URL may change someday if the technology implementing the web resource changes.

Greg Malcom presented SAPS DAME which is an acronym for:

Scalability
Availability
Performance
Security

Deployability
Accessibility
Maintainability
Extensibility

Though I think he actually presented it as PASS MADE.

Sorry, it’s getting late.

CONDG Meeting – February 2009

This month’s meeting of the Central Ohio .NET Developers Group featured a presentation by Jon Kruger on unit testing and test-driven development in JavaScript.

Links related to the presentation:

Other links from the meeting:

Related search results:

I also saw someone playing with something called Microsoft Tag on their mobile phone. What’s that?

Delphi Users Group Hosts David I

For a number of years, from the mid-1980s and through the 1990s, I made a living writing software mostly using Turbo Pascal and its descendants, Borland Pascal (with Objects) and Delphi. I was interested when I heard that Rick Ross of the Central Ohio Delphi and .NET Developers Group was going to host a presentation by David Intersimone. David “I” worked for Borland as Vice President of Developer Relations and Chief Evangelist before Borland sold their developer tools division, CodeGear, to Embarcadero in June, 2008. David now works for Embarcadero and is on the road for their Delphi 2009 Productivity Tour.

In its early years, Delphi paved the way for competing RAD tools such as Visual Studio. Unfortunately (for us lowly developers anyway) Borland fell in love with big enterprise bucks and even changed its name to Inprise for a while. Borland was known as a developer tools company and the management at the time probably thought the name change would help break them out of that box. “Look, were not a Bor-ing developer tools company, we’re meant to be in your enterprise!” They eventually changed their name back to Borland (Borland Software Corporation) but did not lose their appetite for enterprise bucks. They did not seem to invest much in their developer tools but focused instead on ALM (an acronym I assume is taught to managers – stands for Application Lifecycle Management).

Picture of Rick Ross and David Intersimone
Rick Ross and David I

I attended the meeting on Wednesday, February 18th, in Dublin Ohio. The first thing I noticed about David I is that his beard is much more awesome now than in the pictures I’ve seen. David presented some demos of Delphi 2009 and Delphi Prism. Some of the demos are also presented by Nick Hodges in this video which is introduced by David I (check out David’s green tie-dye against the blue background – groovy, man!). The presentation was good but I enjoyed the “war stories” from his years at Borland even more. It’s also nice to see Embarcadero spending some cash on promoting their developer products.

David I’s blog: Sip from the Firehose

CONDG Meeting – November 2008

Found my notes from the November CONDG meeting and decided to go ahead and blog some links for future reference. I did not attend the December meeting.

Mark Mydland presented Visual Studio Team System (VSTS) 2010. I see that Jeff Hunsaker already posted a nice (and much more timely) summary of the presentation so I will not rehash that here.

I noticed Mark was using something called SlickJot. I found it is a feature of a floating command line utility named SlickRun.

Also mentioned at the meeting was Peter Provost – software architecture tools.

CONDG Meeting – October 2008

Here are some links from notes taken at the October meeting of the Central Ohio .NET Developers Group:

Meeting dates were announced for the Columbus Architecture Group and MDC Detroit.

James Johnson of Data Dynamics gave a demonstration of the Data Dynamics Analysis component.

Alex Moore talked about Parallel Extensions to the .Net Framework and gave a brief but cool demonstration that used ray tracing as the computational load to show the difference in execution time with and without parallel functions.

Kishore Patel of Cyber-SWIFT talked about GIS mapping applications with .NET. He gave Buckeye Traffic as an example of such an application.

Steve Horn talked about jQuery. Steve also spoke highly of Firebug.

Mel Grubb who works for Quick Solutions talked about the Windows Workflow Foundation (WF) rules engine and how to do stuff with it that I have no clue about. I have not worked at all with WF so I don’t see the big picture that would give context to the demo.

Greg Malcolm talked about Mono and showed a few examples. His PC was running Kubuntu. When he fired it up I wondered if there would be any moaning and groaning from the crowd of Microsoft developers. I didn’t hear any (and no objects where thrown either). I suspect many in attendance have an old PC at home with some flavor of Linux on it. Greg mentioned GtkSharp and MoMA, a tool to help with migrating .NET applications to Mono.

Craig McKeachie and Tim Hibner talked about Sitefinity, a Content Management System.

This was not a complete list of presenters (nor was it in order of presentation). Just some notes.