Search This Blog

Monday, 5 May 2008


I promised earlier in my post on PhotoShop to write something about ImageMagick. ImageMagick is a great Open Source tool for people who want to manipulate their images. It is free to use, even for commercial purposes, I quote:
It allows you to freely download and use ImageMagick software, in whole or in part, for personal, company internal, or commercial purposes;

There are a few restrictions (read the license) that basically boil down to: "don't pretend you created ImageMagick"

Using ImageMagick to Automate your work

WatchDirectory can be used together with ImageMagick's command-line tools to AutoMagically transform your pictures. WatchDirectory (and our upcoming WatchFTP) has the option to start a batch file for each new (image) file detected.

The most powerful ImageMagick command-line tool is the convert command, but I MUST WARN YOU before you use it...

A Warning about Convert

When you enter the CONVERT command on a command prompt, strange things may happen... Windows itself has a command called CONVERT, and it is a rather painful command if you, by accident, get its command-line options right... What the Windows CONVERT command does is a complete transformation of a disk to NTFS format (could take hours). In the below examples I make sure to enter command-lines like

"C:\Program Files\ImageMagick-6.3.7-Q16\convert.exe" bla-di-bla
and NOT
convert bla-di-bla
as that may, by accident, start the disk-convert program, instead of the picture-convert program... Anyway, it is always a good idea to use "full paths" in batch files (and always enclose them in "quotes", more on that in a future post)


I will give you a few very short, sweet examples of using ImageMagick's convert command in a batch file that is started by WatchDirectory's Automatically Run A Batch File plugin. This blog-post will do just one, and tries to explain all caveats, workarounds and tricks. Future posts will concentrate more on features (how to do "x" with ImageMagick/WatchDirectory).

Convert from JPG to PNG

Your batch file should look like this:

"C:\Program Files\ImageMagick-6.3.7-Q16\convert.exe" "%WD_FILE%" "%WD_FILE_D%%WD_FILE_B%.png"

What does the above translate to? All variables (like %WD_FILE%) are explained on this page. Ok, just for this first example, I'll tell you:
%WD_FILE% - This is the complete path + filename of the file that is detected by WatchDirectory, for example C:\Monitored\Subdir\Hello.jpg.
%WD_FILE_D% - The directory where the new file is found, C:\Monitored\Subdir\
%WD_FILE_B% - The filename without its extension, Hello in this case.

So, just try to see what happens when WatchDirectory starts this script for the file C:\Monitored\Subdir\Hello.jpg... The command that Windows will perform is actually (after replacement of all %VARIABLES%):

"C:\Program Files\ImageMagick-6.3.7-Q16\convert.exe" "C:\Monitored\Subdir\Hello.jpg" "C:\Monitored\Subdir\Hello.png"

Now, isn't that quite simular to the first example of ImageMagick's Convert program??? You can now probably use a lot of ImageMagick's examples together with WatchDirectory, but please don't leave yet, read on... I have more to say...

A small problem...

Did you notice that the above batch file will create the PNG file in the same directory as where the original JPG is found? WatchDirectory will detect the new PNG file and start your batch file for that file again. So, your batch file is started for the file C:\Monitored\Subdir\Hello.png and Windows will run your script as

"C:\Program Files\ImageMagick-6.3.7-Q16\convert.exe" "C:\Monitored\Subdir\Hello.png" "C:\Monitored\Subdir\Hello.png"

mmmm, it could even get worse as ImageMagick will gladly convert a PNG to PNG, and WatchDirectory will now see a changed file and start your script again (and again, and again....)

One obvious solution is to create the PNG outside the monitored directory, in that case you would use this script:

"C:\Program Files\ImageMagick-6.3.7-Q16\convert.exe" "%WD_FILE%" "C:\OtherDirectory\%WD_FILE_B%.png"

That would work.... but if you really need to have the PNG in the monitored directory, you need to make sure the script ignores files that are "already PNG":

"C:\Program Files\ImageMagick-6.3.7-Q16\convert.exe" "%WD_FILE%" "%WD_FILE_D%%WD_FILE_B%.png"

The "IF /I "%WD_FILE_E%" EQU "PNG" GOTO :EOF" will exit the script for files with a PNG extension.
%WD_FILE_E% - the file extension in UPPERCASE

Another option would be to configure WatchDirectory to ignore PNG files for this task.

Remove the original file after converting to PNG

Yes, I saw that coming, please try this batch file:

"C:\Program Files\ImageMagick-6.3.7-Q16\convert.exe" "%WD_FILE%" "%WD_FILE_D%%WD_FILE_B%.png"

Coming next, another ImageMagick script?

ImageMagick is really a great tool to work with/transform your pictures. Doing this together with WatchDirectory allows you to Automate it! There will be future ImageMagick posts here!

1 comment:

Gert said...

Forgot to mention...
Obviously, you can also use the above scripts to convert from GIF (png, bmp...) to PNG (jpg, gif) etcetera