Dr. Geeklove, or How I Learned To Stop Worrying And Love The CLI

Since the rain we’ve had for the past few days has given my poor, elderly body a respite from the springtime demands of grounds maintenance, I decided to catch up on some geek stuff. I spent all of yesterday engaged in a titanic, life-or-death struggle with ImageMagick, a command-line image-manipulation program.

Well… maybe not life-or-death, since that would imply that one of the parties would not survive the encounter. And while I might have used a few bad words in the midst of yesterday’s struggle, I’d be very unhappy if ImageMagick died. The more I learn to use it, the more I learn to love it.

For those of you unfamiliar with the concept, the command-line interface (CLI) was all that was available before the Graphical User Interface (GUI) was popularized, first by Apple and then by Microsoft. This is a screen shot of a terminal (CLI) session on my computer:


shot_070519_0901.png

Where’s the mouse pointer?


Please note the dearth of pretty pictures and mouse-clickable items.

It may seem is definitely counter-intuitive to think that images may be manipulated from the CLI. I had trouble accepting the concept at first, but I’ve become more comfortable with it as I’ve become more experienced. Of course, this is all driven by my aversion to giving Microsoft any more of my money to allow me to temporarily access my intellectual property that I’ve created on my computer. I have a powerful motivation to learn this stuff.

Anyhoo (as I wipe the spittle from my monitor), what I wanted yesterday was a non-labor-intensive way to add a watermark to each image in a directory. For most open-source projects, there is a wealth of documentation. And most open-source documentation has been written by people with a limited grasp of the English language (be they native English-speakers or not) and/or a limited grasp of real-world applications of said projects and/or a limited grasp of what lesser and non geeks might be willing or able to understand. ImageMagick fares better than most in this regard, but the complexity and scope of the program makes the documentation difficult to wade through.

After beating my head against the wall of command-line tools, settings, options, operators and sequence operators, I stumbled across a link on ImageMagick’s home page to an online document titled “Examples of ImageMagick Usage”1. This document is itself large and complex, but it contains specific examples of command-line processing for a plethora of usages. Within the document, I found a link to Annotating Images, which held a section on Watermarking, which was just what I needed.

After I decided which method of watermarking I thought would work best for me, all I had to do was copy the commands in my terminal to produce the watermark “stamp.” That’s when the real fun started.

ImageMagick’s command-line tools, “composite” and “convert” (which takes “composite” as an image sequence operator), will only work for one image at a time. The command-line tool “mogrify,” which will apply itself to multiple images, will not take “composite” as an argument. I had run into an impenetrable wall.

But even if a wall is impenetrable, there’s usually a way over, under or around it.

Enter Bash scripting.

This is a geek’s tool I like so much that it’s almost embarrassing to stand up while I’m thinking about it. Bash scripts allow one to string together commands, test for conditions, execute commands and programs based on those tests, and lots of other stuff. They allow me to automate repetitive or complex tasks (such as cleaning out my log files and emailing me the results), or execute them simply from the command line (such as backing up my browser’s bookmarks or moving images from my digital camera to my hard drive).

I am not, however, an advanced scripter. It took me another few hours of struggling with for loops and string manipulation to finally come up with a solution.

I started at about 10 in the morning and spent about 8 hours total on this, when I subtract mealtimes, afternoon nap and goofing off. At 2201 last night, I executed my “watermark”2 script one more time. Within a few seconds all the images in the target directory had been watermarked.

Ta-daaa:


w_20070506_045.jpg

Skunk cabbage and fiddleheads in a Chappaqua, NY swamp.


I may change it later to tile the watermark, but the bulk of the work is done.

Okay, so it was a lotta work to get here. But now all I have to do is go to my CLI, navigate to the relevant directory, type the word “watermark” and hit “Enter.” I was gonna say that it was a bunch of pain up front for a bunch of ease later, but I had too much fun in the process to call it pain.


Footnotes:

1. While you’re at the “Examples” page, it’s worth a side trip to follow the link to Anthony’s Graphics Lab.

2. For those interested, here’s the “watermark” script:

#!/bin/bash

# This script adds a watermark to each image file in a directory.

for file in *

do
composite -gravity center -geometry +0+10 /home/cj/foto/worktable/stamp.png ${file} w_${file}
done

exit 0

Not much to show for all that work, is it? If you wanna use it, change the directory path to reflect your work environment. Don’t forget that you’re executing this script from the directory full of images you want to watermark. And if you wreck your images, it’s your own darn fault for taking advice from me.

Update at 070520/2023: Doh! I reinvented the wheel.

Advertisements

4 Responses to “Dr. Geeklove, or How I Learned To Stop Worrying And Love The CLI”

  1. S. Weasel Says:

    Ha! I used to do 3D modeling on the command line, with Persistence of Vision. You’d say things like: sphere . It was a booger, but it was a beautiful raytracer.

  2. S. Weasel Says:

    Oops! Sorry…I put in angle brackets. Let me try that with character entities.

    sphere <0,0,0,> <30,30,30,><red,shiny>

  3. lizardbrain Says:

    Cool stuff. I seem to recall running across some mention of POV during all the research I was doing.

    All the image manipulation I do is minor crap to the images my camera produces. Maybe some day I’ll get brave and give real image creation a shot.

  4. If I Keep Going Back to the Well… « lizard brain Says:

    […] day I went back to Imagemagick and actually read some of the documentation. Some of you know from a previous post that Imagemagick is a command-line only tool. That means no fancy GUI, no mouse, no point-n-click. […]

Comments are closed.


%d bloggers like this: