Convert RAW images into TIFF with a script

Q I'd like to have a simple command to convert all the RAW images in a folder, let's say /home/andy/photographs/new, into TIFF with LZW compression. I don't know if it's relevant, but the extension for the RAW files is .PEF - (the Pentax RAW format).

A The program for converting digital camera RAW files is dcraw (www.cybercom.net/~dcoffin/dcraw). This converts from most digital camera raw formats to the NetPBM PPM (Portable PixMap) format. You then need another program to convert the PPM data to TIFF. ImageMagick's convert command and the pnmtotiff program from the NetPBM suite are both capable of doing this. The easiest answer to the question of which to use is "which one is already installed?" Dcraw should be in your distro's repository, otherwise download the source and compile/install in the usual way. There's no need to fill your hard disk up with PPM files when converting (PPM is a big, uncompressed format using three bytes per pixel), you can pipe the output of dcraw directly to the conversion program. To convert a directory of files using ppm2tiff, run this in a terminal.

for f in ~/photographs/new/*.PEF
do
dcraw -c "$f" | pnmtotiff -lzw >"${f/.PEF/.tif}"
done

The -c argument tells dcraw to send the data to standard output, which is piped to pnmtotiff's standard input. The ${f/.PEF/.tiff} part provides the name of the original file with the PEF extension replaced with tif. To use ImageMagick instead, replace the third line with

dcraw -c "$f" | convert -compress lzw - "${f/.PEF/.tif}"

In this case, convert uses the standard notation of "-" for standard input. The imagemagick, dcraw, netpbm, and convert man pages detail various extra options you can add to fine-tune the process. This assumes all the PEF files are in the same directory. If your camera stores them in sub-directories, use the find command to generate a list of the names. It is also possible to store the converted images in a different directory, thus:

find /mount/pointof/camera -name '*.PEF' | while read f
do
dcraw -c "$f" | convert -compress lzw - "~/
photographs/tiff/$(basename "$f" .PEF).tif"
done

This time we use the basename command to extract the base filename from the full path and remove the extension. The quotes around the various filenames are there in case any file or directory names contain spaces.

Back to the list