Using ImageMagick for batch image processing in Picasa

07/27/2011
tags: , ,
Olexandr Savchuk

Our office is using Google Picasa to process a lot of images. We’ve used Picasa for years now, and it’s a very convenient instrument for our needs. However, while it has many built-in functions to process and modify images, there are some missing, or only available through complicated multi-stage user interaction, which is unacceptable for the amount of work our employees have to get done. My solution to this is a combination of two ingredients: the command-line ImageMagick toolkit for image processing, and Picasa’s Button API.

The example application I’ll explain here is a utility to combine multiple images into one in one click directly from a Picasa folder view. However, it is very easy to modify this to perform various other batch image processing tasks directly from Picasa.

Part 1: The Picasa button

First of all, we will need a button in our Picasa to start our ImageMagick script. To create the button using Picasa Button API, we follow these steps:

  1. Write a PBF file.
    A PBF is basically a simple XML file, that describes our button to Picasa. Here is the PBF file I wrote for my application:

    <?xml version="1.0" encoding="utf-8" ?>
    <buttons format="1" version="1">
      <button id="{ca234ae3-6340-40c3-a46b-51a126bb887c}" type="dynamic">
        <label>Combine</label>
        <tooltip>Combine images vertically and save as a new image</tooltip>
        <action verb="trayexec">
          <param name="exe_name" value="stitch.cmd" />
          <param name="exe_path" value="S:\Tools\" />
        </action>
      </button>
    </buttons>
    

    Although the whole code is contained within a buttons element, the file can currently only describe one button.

    Note the GUID in the button/id attribute. You’ll need a new GUID for every button you make. The PBF must be named using this GUID, {ca234ae3-6340-40c3-a46b-51a126bb887c}.pbf in my case.

    The label and tooltip elements shouldn’t need much explanation. The next important element is action; this describes what the button will actually do when pressed. There are a few actions described in the Button API; the one I use here is a simple executable launch, that starts a batch script stitch.cmd, located in S:\Tools\ (in our environment, S:\ is a shared network drive on the office server). The script will be detailed further on.

  2. (optional) Draw an icon.
    You can include a custom icon for your button. The icon has to be an Adobe Photoshop PSD file called using your GUID ({ca234ae3-6340-40c3-a46b-51a126bb887c}.psd for me), in a single layer. If you use an icon, you have to include an additional element in your PBF, inside the button element:

    <icon name="{ca234ae3-6340-40c3-a46b-51a126bb887c}.psd/layer" src="pbz"/>

  3. Package your button into a PBZ file.
    A PBZ file is nothing different than a ZIP archive containing your PBF (and the icon, if you want to use one), with its extension changed to .pbz. This file is basically your distribution package, so name it appropriately, since Picasa differentiates its buttons by their PBZ file names. One PBZ can also contain multiple PBF files for multiple buttons at once.

Part 2: The ImageMagick batch script

Since the ImageMagick package provides command-line utilities, the simplest way to automate them in Windows is a batch file. Here is my stitch.cmd script for merging multiple images together vertically, resizing them all to match the first one’s width:

@echo off
cd /D "C:\Program Files\ImageMagick-6.7.1-Q16"

rem --- Output filename
set OUTFOLDER=%~dp1
set OUTFILENAME="%OUTFOLDER%Collage.jpg"

rem --- Imagemagick
for /f "delims=" %%a in ('identify -format "%%[fx:w]" %1') do @set WIDTH=%%a
montage -mode concatenate -resize %WIDTH% -tile 1x %* %OUTFILENAME%

When called from the Picasa button, the script will get filenames of all currently selected images in Picasa as arguments, including full paths.

First, we change directory to where ImageMagick is installed, to avoid possible problems with PATH. Then we generate a filename for our montage, taking the folder path from the first argument (first selected image from Picasa) and adding our own filename.

After that, we apply a batch hack, using a for loop to save a command output (ImageMagick’s identify) into a variable. This gives us the width of the first image. Next, we call the ImageMagick’s montage tool, that will combine all our images in one, additionally providing it with the output filename as a last argument.

Part 3: Installation

The installation is a bit tricky. Picasa doesn’t support importing the buttons directly, it can only be done using a browser and a specially formed link to the file. You’ll need to place your PBZ at some location reachable via an http:// link; since I have a local development server running on my PC, I used that. Here is an the HTML file I used to install my script in Picasa:

<html>
 <body>
  <a href="picasa://importbutton/?url=http://localhost/olex-stitcher.pbz">Install</a>
 </body>
</html>

Just open the file in a browser of your choice and click the link. Picasa will open and prompt you to select the button you want to install (only one option here, unless your PBZ contains multiple buttons), and where to place it in the button bar. Do that, and you’re all done.

No comments yet.

Leave a Reply