Cook your RAW photos into JPEG with Linux

NOTE: This is a ported post from the old Mutaku site. Original post information:
Thursday, August 14 2008 @ 04:50 PM EDT Contributed by: xiao_haozi

Shooting in RAW format is great for the photography buff. However, when it is time to share with others, post on your photo gallery, or print at the local photo printing shop, you want something more portable. Here we’ll look at converting RAW photos (specifically Canon’s CRW format) into JPEGs using a batch command-line approach in BASH.

We will be using two tools to do our batch conversion:

1- DCRAW (available here)

2- CJPEG (available here)

CJPEG is part of the standard JPEG library (libjpeg) so you should already have that installed. If not you can find the source here and built as such:

./configure --enabled-shared
 make install (as root, e.g. sudo make install)

DCRAW is available here and can be built as such:

tar xzvf dcraw-8.87.tar.gz
cd dcraw-8.87
make install (as root, e.g. sudo make install)

Now that we have the prerequisites out of the way, we can try it out. We’ll manually do the conversion to ensure it works. Make sure you then compare the images and ensure they meet your needs/desires with the settings we use below.

In our example we will assume we have a directory called trip08 on our user’s desktop that contains some CRW raw images as well as others. We will go into this directory, make a CRW directory to copy our RAW images to for converting, search here and all subdirectories for CRW files and copy to our new directory, and then convert them using the two tools above so we will have in our new directory a copy of the original RAW CRW image as well as a new JPEG version.

Here we go:

cd ~/Desktop/trip08 && mkdir CRW_conversions
find . -name \*.[Cc][Rr][Ww] -exec cp "{}" CRW_conversions/ ";"

First, we went into our trip photo directory and made a directory called ‘CRW_conversions’ that we will work within. Next, we looked in the current trip directory (and subdirectories) for all files that end in ‘.crw’ including and variations on capitalization. The files we found are transfered to our CRW_conversions directory.

cd CRW_conversions
for i in *; do dcraw -c "$i" | cjpeg -quality 100 -optimize -progressive > $(echo $(basename "$i" ".crw").jpg); done

Now we went into our CRW_conversions directory that contains copies of our RAW images. We then constructed a loop which:

1- for i in *; do for each element in this directory do the following
2- dcraw -c “$i” | use dcraw to decode the element and pipe it out
3- cjpeg -quality 100 -optimize -progressive > accept the pipe from dcraw with cjpeg and convert to 100% quality jpeg with optimization and progressive processing and output that
4- $(echo $(basename “$i” “.crw”).jpg) take that output and put in a file called element.jpg where element is the name of the original CRW version without the extension ‘.crw’
5- ; done finish the loop once the iterations are done

We should now have a directory called CRW_conversions in our trip08 directory that contains copies of all our RAW images as well as fresh versions of each in JPEG format. The settings we used to convert have given us fairly high quality images that are still relatively large, so you may wish to alter these to your liking. You can do so by altering the CJPEG part of the conversion and find the options here:

man cjpeg

If you find that you have errors of ‘Empty input file’ and resultant JPEG files that are 0 bytes in size, you most likely have left out the ‘-c’ switch in your DCRAW part. This is needed to send the output with the pipe, or else you will end up with PPM files.

You can place most of this code into a shell script to do the brunt of the work for you without having to type in each time. We are currently working on a Python version which uses Tkinter to provide a GUI interface as well as options to convert and manipulate several image formats and will post source code and a how-to once it is finished. However, if you want to try out a shell version now you might want to try something like:

#!/bin/env bash
# crw2jpg                                                           
#      convert canon raw into jpeg                       
mkdir CRW_conversions
find . -name \*.[Cc][Rr][Ww] -exec cp "{}" CRW_conversions/ ";"
cd CRW_conversions
for i in *; do dcraw -c "$i" | cjpeg -quality 100 -optimize -progressive > $(echo $(basename "$i" ".crw").jpg); done

This is a very basic script without any real error control or logic, so add to it as you wish/see fit. If you wish to keep as such, you should just make sure that you don’t already have a conversion that was run in the existing directory. To run this:

#(copy the above script)
nano ~/bin/crw2jpg
#(paste and save)
chmod +x ~/bin/crw2jpg
cd to_directory_with_new_photos/

Enjoy cooking those RAW images, and be sure to check back for our conversion GUI. Good luck!