Converting Canon CR2 Images to JPEG and Image Manipulation Techniques

Every camera has their own format of file, for me as a Canon user I get the Canon CR2 files to manage, a raw image format from Canon cameras that offers a lot of detail and flexibility but isn’t as readily usable as standard formats like JPEG. In this blog post, we’ll look at how to convert CR2 files to JPEG and explore other image manipulation techniques using command-line tools.

Converting CR2 to JPEG

Let’s first look at how we can convert Canon’s CR2 files into more usable JPEG files. We’ll be using two tools, ufraw and imagemagick, both of which are easily installable via Homebrew:

brew install ufraw
brew install imagemagick

ufraw is a utility to read and manipulate raw images from digital cameras, and imagemagick is a robust collection of tools and libraries to read, write, and manipulate an image in various image formats.

After installation, navigate to the directory containing your CR2 files and run the following command:

find . -iname "*.CR2" -exec convert '{}' '{}'.jpg \;

This command recursively finds all the CR2 files and converts them to JPEG format using the convert tool from imagemagick.

Image Resizing

imagemagick isn’t just for format conversions; it also allows us to perform various image manipulations like resizing.

Resizing to a Specific Width

If you want to resize an image to a specific width while keeping the aspect ratio, use the following command:

convert image1.gif -resize 300x image2.gif

Resizing to a Specific Height

Similarly, for resizing to a specific height while keeping the aspect ratio:

convert image1.gif -resize x200 image2.gif

Resizing Within a Width and Height Container

If you need to fit an image within a specific width and height while keeping the aspect ratio, use:

convert image1.gif -resize 300x200 image2.gif

Resizing by Percentage

To resize an image by a certain percentage, use:

convert image1.gif -resize 50% image2.gif

Handling Animated GIFs

Animated GIFs require an extra step before resizing: they first need to be coalesced. Coalescing an animated GIF undoes all optimizations that may be present in the original GIF, expanding all frames to full size:

convert image_animation_1.gif -coalesce coalesce.gif
convert coalesce.gif -resize x200 image2.gif

Reducing Colors

To reduce the color depth of an image, imagemagick provides a simple command:

convert img1.png +dither -colors 256 img2.png

You can replace 256 with any number depending on how many colors you want in the output image.

Image Cropping

For cropping an image to a certain size with a specific starting point, use:

convert -crop 240x160+100+50 convert-crop-img1.jpg convert-crop-img2.jpg

Image Compression

Image compression is a method to reduce the image file size without degrading the quality of the image below an acceptable level. For this purpose, we’ll use jpegoptim:

jpegoptim --size=500k /home/net2_admin/lion.jpeg

To compress all JPEGs in a directory by a percentage, use:

for i in *.jpg; do jpegoptim -m70 "$i"; done

Remove -n for actual execution; it’s there for a dry run.

Photography isn’t just about capturing; it’s also about proper editing and presentation. By knowing these image manipulation techniques, you can ensure your photographs are displayed exactly as you want them to be, regardless of the format they were captured in.



Buy Me a Coffee