Search This Blog

Showing posts with label ImageMagick. Show all posts
Showing posts with label ImageMagick. Show all posts

Tuesday, 5 January 2010

Use File Viking to resize your pictures

The current File Viking beta doesn't include an Action to resize pictures yet. However, if you configure the Run action, it is easy to write a script to call, for example, ImageMagick to resize pictures you drop on a File Viking basket.

The script can use environment variables so it knows which files were dropped on the basket. The variable %FV_FILE% contains the full path to the file dropped on a basket, %FV_FILE_B% contains the "title" of the file without its extension. Below is the example script.

rem Where did you install ImageMagick (www.imagemagick.org)
SET IMA=C:\Program Files\ImageMagick-6.5.8-Q16

rem Where should resized pictures be stored
SET OUTDIR=F:\Testing\Target

rem call convert, make pictures half their original size. As % is a special character in
rem batch scripts, we need to "double" them.
"%IMA%\Convert.exe" "%FV_FILE%" -resize 50%% "%OUTDIR%\%FV_FILE_B%_50percent.png"

rem call convert again, making pictures fit into a 200x200 rectangle while keeping the
rem correct aspect ratio.
"%IMA%\Convert.exe" "%FV_FILE%" -resize 200x200 "%OUTDIR%\%FV_FILE_B%_200x200.png"

And below you see 2 pictures showing how to configure the basket. First we add a Filter action (new in yesterday's beta) so only pictures are allowed for the piped Run action.

The Run action is configured to run a script called F:\testing\fv_resize.bat, this is the script shown above.

This is just a small example how you can create your own actions for File Viking. If you can imagine it, you can do it.

Saturday, 11 April 2009

Convert TIFF to PDF

A request we often get is a way to convert TIFF files to PDF with WatchDirectory.

While WD does not have a native way to do this, you should be aware that WD's "Automatically Run a Batch File" plugin can do almost anything. If there is a command line tool available, WatchDirectory can "run it".

Anyway, we have found 2 ways to convert TIFF to PDF using command line tools. The batch scripts below should be started by WD's "run a batch file" plugin. It is also a good idea to create a filter so WD will only start the script for TIF and TIFF files.

ImageMagick and Ghostscript

ImageMagick is one of my favorite tools for image manipulation. If a format is not "natively" supported by ImageMagick, it can delegate to other tools. ImageMagick needs Ghostscript for PDF support.

Downloads needed:
ImageMagick, I currently use v6.5.1, available here: http://www.imagemagick.org/script/binary-releases.php#windows.
Ghostscript, I currently use v8.6.3, available here: http://mirror.switch.ch/ftp/mirror/ghost/GPL/gs863/.

The ImageMagick/Ghostscript script. Note you don't see any reference to Ghostscript, but it must be installed otherwise the script won't work.

@echo off
IF "%WD_FILE_E%" NEQ "TIF" (
   IF "%WD_FILE_E%" NEQ "TIFF" GOTO :EOF
)
rem where is ImageMagick installed???
SET IMADIR=C:\Program Files\ImageMagick-6.5.1-Q16

"%IMADIR%\convert.exe" "%WD_FILE%" "%WD_FILE_D%\%WD_FILE_B%.pdf"
IF %ERRORLEVEL% NEQ 0 GOTO :EOF

rem if you want to remove the tiff after it has been converted
rem uncomment (remove the "rem") the next line
rem DEL "%WD_FILE%"

Irfanview

Another great tool, I didn't even know it supported command line conversions until Dirk (German language support) told me. You can download Irfanview here.

The Irfanview script:

@echo off
IF "%WD_FILE_E%" NEQ "TIF" (
   IF "%WD_FILE_E%" NEQ "TIFF" GOTO :EOF
)
rem where is Irfanview installed???
SET IMADIR=C:\Program Files\irfanview

"%IMADIR%\i_view32.exe" "%WD_FILE%" /convert="%WD_FILE_D%\%WD_FILE_B%.pdf"
IF %ERRORLEVEL% NEQ 0 GOTO :EOF

rem if you want to remove the tiff after it has been converted
rem uncomment (remove the "rem") the next line
rem DEL "%WD_FILE%"

IrfanView settings

Current releases of IrfanView will prompt you with a little popup window (to preview the PDF). Obviously, you don't want that when you are automating the creation of PDF files.

Just start IrfanView, open a TIFF file and save it as PDF. Next to the "Save As" window, where you enter the pdf filename, is a little window where you should select the "not needed" option. From now on, the script above will not cause those prompts.

Monday, 5 May 2008

Magical....

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)

Continuing...

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":

IF /I "%WD_FILE_E%" EQU "PNG" GOTO :EOF
"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:

IF /I "%WD_FILE_E%" EQU "PNG" GOTO :EOF
"C:\Program Files\ImageMagick-6.3.7-Q16\convert.exe" "%WD_FILE%" "%WD_FILE_D%%WD_FILE_B%.png"
DEL "%WD_FILE%"

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!