Geek tip: Running Python GUIs in Sublime Text 2

I’m trying out Sublime Text 2 as a code editor on Windows, since there really aren’t any other free ones I like; unfortunately, my favorite free editor, Gedit, is abysmal on Windows. (Although it looks like something may be done about that soon.) Sublime works really well and has a distraction-free mode, and the extensibility is amazing. I’m not convinced yet that it’s worth $60 for a license — I’d probably pay $25, come on, it’s a text editor — but perhaps if I got a job where I needed text editors more often, I’d try to convince my boss to spring for it. (Although in that case, I’d more likely try to convince my boss to let me work on a platform where I could run Gedit.)

One thing that confused me is that I couldn’t get any of my Python GUI code to work on it. For example, when I’m at work in my lab, I often make small changes to my laser beam profiling program (although really it’s more of a camera driver right now; I haven’t found an excuse to actually write any Gaussian profiling code.) As far as I could figure out, I ought to be able to press Ctrl+B in Sublime Text to run the program. However, nothing ever happened, and I thought the feature must be broken.

Then one time I tried running another non-GUI Python script from Sublime Text. To my surprise, it worked! The feature apparently wasn’t really broken, just selective.

The key to this mystery was the following sentence from the Sublime Text 2 reference wiki:

On Windows, GUIs are suppressed.

Wait, what the what? Why would anybody want that? I guess so the console window doesn’t show up. Fortunately, it’s easy to remedy. Go to Preferences→Browse Packages, open the Default directory and then open in the editor. Around line 26, you’ll see:

# Hide the console window on Windows
startupinfo = None
if == "nt":
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

Comment out the last line of this block. Now, whenever you start a Python program, the console will be displayed. If you want to get rid of it, make your build system invoke pythonw instead of python. (Tools→Build System→New Build System, then copy Packages/Python/Python.sublime-build and change python to pythonw.)

Geek tip: ImagingSource camera in Python

In my lab there are some ImagingSource cameras that we use for detection. I was trying to get a model DMK 41BU02 to work so that I could control it directly from my measurement program and not have to use the crappy imaging software that comes with it.

Most USB cameras work with the OpenCV computer vision library without any trouble, and this is how I control them in my instrumentation library, which I wrote in Python. If they don’t work, then there is always an ActiveX interface which usually works.

The ImagingSource cameras, however, include their own driver library, IC Imaging Control. I tried to use SWIG to write a quick Python wrapper for it. However, you can only compile your program with this library if you use Visual C++. Fail!

Thankfully, this article (in German) on the blog of one Edgar Klenske tipped me off: the IC Imaging Control library is itself just a wrapper around DirectShow. That also explains why the headers only compile with VC++. Edgar Klenske’s solution is to use the VideoCapture module, which is itself a wrapper around DirectShow, which comes precompiled so you don’t have to use VC++. Luckily for me, he posted that tip just last week!

And so I was able to subclass my Camera module to interface with DirectShow cameras. Perhaps next I’ll try to install the new OpenCV 2.3 to see if their support for DirectShow has improved any.

My verdict is, never buy any camera from ImagingSource. Their slogan is, ironically, “Technology based on standards.” Sorry, but having your drivers only work with one compiler is more like a lack of standards — standards of both technology and decency.