Thursday, December 15, 2011

image magick and text macros

After developing professionally for 15 years, I can appreciate when it's time to apply a quick and dirty solution.

Sometimes it's my only option, a way of covering gaps in my knowledge-- for instance, I've never really had the chance to learn any Adobe product deeply, whether it's Flash, Photoshop, or Fireworks. And I've stuck with my manipulating tab-delimited files in Perl where more normal people might cozy up to Excel.

So at http://alleyoop.com/ we had a widget that would display sample math problems for various subtopics. Each image was a 380x136 png, but most had plenty of whitespace:
We decided we didn't want to so much whitespace... but ideally we wouldn't have to have a design person resize all 204 images. What to do?

Enter ImageMagick. This software has been around for a long while I think. A while back I used its Perl module as part of my image upload feature for my personal blog, so I could crop out extra white space on doodles and resize images to be more web-friendly on the server, rather than doing it locally and then reloading. And there's a handy Windows client.

The syntax is a little strange, but for trimming the file "Volume.png" I'd just do
convert Volume.png -trim Volume.png
I chose the same file name for the output as the input (Weirdly, if I didn't specify the destination filename it would give me a file name "-trim".)

So far, so good. But what' the easiest way to apply this command to all 204 files?

I'm on Windows, and haven't bothered with CygWin, so I've learned a few coping tricks... like "dir /b" gives you a "bare" directory listing of just the filenames. I run the following command:
dir /b *.png > trim.bat
So now I have trim.bat, a flat file of filenames. Then I pull up trim.bat in an editor* and ran a macro that:
  • home to to the start of the line and started the macro recording
  • shift-select to the end of the line
  • ctrl-x to cut the filename
  • type 'convert "'
  • ctrl-v to paste the filename
  • type '" -trim "'
  • ctrl-v to paste the filename
  • type '"'
  • down arrow to the next line, then home to jump to the start of it
  • end macro
Then just hold down the macro replay key, and BAM, I have my ready to run batch file. Run it and my job here is done. (Macros are great, super fast to make with almost nothing to learn, just sometimes you have to think about the general case of what you're trying to do. Every developer should intimately know at least one editor with one button macro playback as well as a small set of text manipulation tools, like either Perl or awk and sed.)

* on editors... lately I've been using Komodo Edit, despite its surprisingly long loading times. I kept my ancient copy of TextPad handy though, in part because its macro recording and playback was rock-solid and I sometimes had issues in Komodo Edit. But now it looks like Notepad++ is what the cool kids are using, at least the ones not "cool" enough for developing on Mac. Can I say though, that given most Mac laptop's half-assed keyboard support (the lack of equivalents, or at least general obscurity, of simple keystrokes for Home, End, and ctrl-arrow to jump words) that I find this kind of thing much easier to do on Windows, and it's yet another reason I'm unlikely to switch, at least for my work stuff...



2 comments:

  1. I've been trying out Sublime Text 2 recently as my editor of choice; it's gotten pretty slick.

    ReplyDelete
  2. Interesting!
    I've been using Notepad++ a bit lately

    That zoom view of Sublime is kind of compelling, I need to figure out how much I like it, if it's worth paying for...

    ReplyDelete