47

I need to convert a lot of CR2 photos to either JPG or PNG, no editing. How to do this?

12 Answers 12

57

I'll go a different route... Use ufraw-batch not ufraw.

sudo apt-get install ufraw-batch

## This will output (not replace) the file with a new extension.
## foo.CR2 exported to foo.png
ufraw-batch --out-type png *.CR2

See ufraw-batch --help and man ufraw-batch for more info.

6
  • 2
    ufraw-batch is awesome!
    – jemiloii
    Commented Sep 13, 2016 at 20:00
  • ufraw-batch --out-type png $(ls IMG_93{44..99}* 2>| cat)
    – smac89
    Commented Feb 19, 2018 at 0:07
  • 8
    In some cases, ufraw-batch leads to a segmentation fault. It will successfully develop one RAW file, and then it stops. See bugs.launchpad.net/ubuntu/+source/ufraw/+bug/1768855 for further information.
    – user258532
    Commented Sep 14, 2018 at 12:13
  • this works like a charm! Commented Aug 15, 2020 at 7:44
  • 1
    E: Package 'ufraw-bach' has no installation candidate
    – pnkjmndhl
    Commented Sep 15, 2022 at 14:05
19

ufraw alternatives

The accepted answer recommends to use ufraw, but ufraw's development has ceased as of June 2015 and it's not supported by current Ubuntu releases. See Ubuntu Bugtracker, which recommends darktable or rawtherapee as alternatives. Both tools are GU tools, but it is possible to use them from CLI.

darktable: manual, man page

Batch processing example:

for pic in *.CR2
do
     darktable-cli "$pic" "$(basename ${pic%.CR2}.jpg)"; 
done

rawtherapee: manual

3
  • 1
    This worked for me, unlike ufraw, dcraw and mogrify, which all outputted very pink images. Commented Oct 6, 2020 at 9:25
  • problem with darktable-cli is that your can't run concurrent processes and it's pretty slow. If you're looking to make previews, exiv2 -ep is an alternative, if the raw files contain embedded jpegs
    – CervEd
    Commented Aug 5, 2021 at 17:53
  • 1
    darktable did the job as of today, Sept 13, 2021. Sad the many once popular answers are merely the sign of the bygone times of ufraw support.
    – AdamO
    Commented Sep 13, 2021 at 20:44
14

Ufraw

you can convert .cr2 to .jpeg by ufraw.

sudo apt-get install ufraw

Right click on the file and select open with ufraw.

** You can also import them to Gimp with gimp-ufraw and then export as .png or .jpeg.

sudo apt-get install gimp-ufraw
2
  • Didn't work for my photos shot on Canon 700D. Unable to open file of type CR2.
    – Greg
    Commented Aug 11, 2019 at 20:54
  • 3
    Ufraw is dead since 2017 and not included in ubuntu any more since xenial.
    – JPT
    Commented Jul 27, 2020 at 8:49
7

For another alternative, use mogrify:

mogrify -format png *.cr2
3
  • 4
    it should be stated that mogrify is part of imagemagick, which is available at imagemagick.org. however, mogrify uses ufraw-batch in the background, so might as well use that directly Commented Apr 22, 2018 at 9:29
  • 4
    +1 as mogrify circumvents the bug in ufraw-batch that leads to a segmentation fault (asper Sptember 2018)
    – Bruni
    Commented Sep 23, 2018 at 12:30
  • This worked nicely. I just wish there was a verbose option because I didn't realize it was succeeding on a long-running job. Commented Nov 29, 2019 at 2:06
3

Try nconvert

As command line tool OR xnconvert as GUI tool

4
  • 6
    Please explain how to use xnconvert.
    – NGRhodes
    Commented Jun 14, 2014 at 22:10
  • This is now a Famous Question. We do not like these sort of answers. This should be deleted.
    – fosslinux
    Commented Aug 2, 2016 at 3:15
  • ./nconvert -out jpeg -truecolors /path/to/images/folder/*.CR2 Commented Jan 15, 2019 at 10:47
  • 1
    It's not a helpful answer, but the xnconvert tool worked better for me than all of the other options. You have to download and install the .deb file (no PPA as far as I can tell) but the conversion process was fast and accurate, better colour reproduction than either ufraw or dcraw and faster than either batch process. Using it is reasonably easy, point it at a directory of CR2 files and tell it where to drop the JPEGs and click the button.
    – delatbabel
    Commented Aug 26, 2019 at 6:27
2

The method that really worked for me:

You need dcraw and ppmtojpeg (install with apt)

for i in *.CR2; do dcraw -c $i | ppmtojpeg > $1.jpg; echo $i done; done

What it does: First convert CR2 to PPM with dcraw passing the output to ppmtojpeg which converts to JPG.

I found this here

2
  • 1
    Since you mentioned apt, it's worth noting that in Debian derivatives, ppmtojpeg in included in the netpbm package (users may be confused if they search for a package with the same name as the binary).
    – Marcus
    Commented Aug 10, 2020 at 15:52
  • Should be: for i in *.CR2; do dcraw -c $i | ppmtojpeg > $i.jpg; echo $i done; done
    – jjoller
    Commented Sep 11, 2023 at 12:19
2

Create a bash file like foo.sh and execute as ./foo.sh in command line:

#!/bin/sh

for i in $(ls)
do
ufraw-batch --out-type png $i
echo "conversion done $i"
done
2

I had trouble with ufraw since it produces a segmentation fault on elementary OS (see https://bugs.launchpad.net/ubuntu/+source/ufraw/+bug/1768855). I combined your answers (thanks!) and finally got a working version.

First get the right command for exiftool (as mentioned by Rafael):

exiftool -s2 -all -b -X -fXMP:XMP test.RAF  | grep Preview

which is in my case not -Composite:PreviewImage but:

File:PreviewImage

So you can use the batch script from Abu:

#!/bin/sh

for i in *.RAF
do
exiftool -File:PreviewImage -b $i > $i.jpg
echo $i
done
2

.cr2 files are apparently some kind of Canon digital camera raw format. Maybe that's what CR stands for?--"Canon Raw".

Anyway, I just recovered a bunch of them from a corrupted SD card from a digital camera, using ddrescue and photorec (installed by sudo apt install testdisk, and here's how I just converted hundreds of recovered .cr2 files to .png and .jpg images:

Update after writing this answer:

[I need to experiment more with this too]

CR2 files appear to be valid TIF files too, so you might also try simply changing the file extension from .cr2 to .tif and then using tools to convert from .tif to .jpg or .png! I just renamed the file extension from .cr2 to .tif and double-clicked it and my image viewer in Ubuntu 20.04 was able to open it just fine.

How to convert a single .cr2 image to a .jpg or .png image

Tested in Ubuntu 20.04.

# First, install ImageMagick
sudo apt install imagemagick

# strip off the .cr2 extension from your file (this is required for some reason
# to make `convert` work)
mv myimage.cr2 myimage

# Create a JPG from that image. 
# - This creates `myimage.jpg`. The original `myimage` image remains intact.
convert myimage myimage.jpg
# Create a PNG image from that image
# - This creates `myimage.png`. The original `myimage` image remains intact.
convert myimage myimage.png

That's it!

Note: if convert won't work, try the fix I mention in my answer here.

How to batch convert hundreds of .cr2 images into .jpg or .png images

In my case, I needed to convert hundreds of images from .cr2 to .png. Here is how I did that as fast as possible, using all 8 of my CPU cores at once:

cd path/to/all/of/your/cr2/images

# Move all .cr2 images into a "cr2" folder
mkdir cr2
mv *.cr2 cr2
cd cr2

# strip the extension (`convert` won't work if you don't do this first, for some
# reason)
# How to strip file extensions in bash:
# https://unix.stackexchange.com/a/180272/114401
for file in *.cr2; do mv -- "$file" "${file%%.cr2}"; done

# Convert them all to PNG images now!
# - I learned about `xargs` from here:
#   https://stackoverflow.com/a/25532027/4561887 and in my answer here:
#   https://stackoverflow.com/a/76910116/4561887
# - Note: `-P "$(nproc)"` says to spawn as many parallel processes as you have
#   CPU threads, since `nproc` returns the number of hardware threads
#   (hyperthreaded "processors", or "cores") your computer has. So if `nproc`
#   shows you have 8 hardware threads, this will spawn 8 simultaneous `convert`
#   operations to speed this up!
ls | xargs -n 1 -P "$(nproc)" -I% convert % %.png

# When done, move all produced .png images to a "png" dir one level up, at the
# same level as the "cr2" dir we are currently in
mkdir ../png
mv *.png ../png/

Going further: how to recover images from a corrupted camera SD card, memory card, drive, or disk (or just deleted files)

(This is how I got all of my .cr2 files above).

See my answer here: Unix/Linux undelete/recover deleted files

References

  1. Converting images from .cr2 to .png or .jpg:
    1. Google search for "linux convert .cr2 to jpg"
      1. Very useful!: where I first learned about using convert to convert .cr2 images to .png. I also learned here that you have to strip the .cr2 extension first to make it work!: https://www.linuxshelltips.com/convert-raw-camera-image-to-jpeg-in-linux/
    2. Where I learned how to strip file extensions in bash: https://unix.stackexchange.com/a/180272/114401
    3. Where I learned how to use xargs to parallelize operations on files listed by ls: https://stackoverflow.com/a/25532027/4561887
  2. My answer where I used xargs with -P "$(nproc)" to unzip many password-protected files at once, in parallel: Stack Overflow: unzip password protected zip in unix
1

You could also program a simple loop in the console.

For example (using the fish console), and assuming the active directory only has RAW files.

set files (ls)
for i in $files
    dcraw $i
end

or

set files (ls)
for i in $files
    ufraw-batch --out-type=tif --out-depth $i
end

I use ufraw-batch that way because it often leads to an error, see https://bugs.launchpad.net/ubuntu/+source/ufraw/+bug/1768855 .

1

In case you need preview the photos as an initial filtering step, here's what I do

  1. install packages: sudo apt-get install -y dcraw netpbm
  2. extract all thumbnails as jpeg: for i in *.CR2; do dcraw -e $i ; done
  3. let my wife filter the images (the jpgs)
  4. run the following .py script (python3 rm_filtered_cr2.py):
   import os
   files = os.listdir()
   for cr2_file in files:
       if cr2_file.endswith('.CR2'):
           thumbnail_file = cr2_file.replace('.CR2', '.thumb.jpg')
           if not os.path.exists(thumbnail_file):
               print("removing %s" % cr2_file)
               os.remove(cr2_file)
0

Use:

exiftool -Composite:PreviewImage -b photo.CR2 > photo.jpg

Longer answer:

ufraw-batch conversion quality is very bad. Imagemagick uses ufraw under the hoods (unfortunately). dcraw is better, but still not great. The best solution I found out was to use exif to extract PreviewImage metadata. I believe that's generated by the camera itself.

Ref: https://www.imagemagick.org/discourse-server/viewtopic.php?f=3&t=6936&sid=9548c421f1bd69f192e632d06ca03dff&start=30#p130949

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .