Downsizing photos
Written 2021-10-19 by Kalle
3 min read | 466 words
Background
After following my workflow for importing and processing images from my DSLR, I usually end up with a directory with a relevant name, e.g. My_photo_session
. The contents of the directory is usually something like:
/My_photo_session
DSC_6494.NEF
DSC_6466.NEF
DSC_6547.NEF
/developed_jpg
DSC_6494.jpg
DSC 6466.jpg
DSC_6547.jpg
I.e. all the raw files and a directory containing the developed jpegs in full resolution. While substantially smaller than the raw files, the jpegs are still quite large. A real life example is:
DSC_6494.NEF // 29.1 MB
DSC_6494.jpg // 7.1 MB
For many uses, the full resolution is unnecessarily large. Therefore, I've written a shell script to automatically generate smaller copies of the developed jpegs.
The script
The following script is saved as /usr/local/bin/downsize_jpegs
:
#!/bin/bash
SIDE=${1:-1280}
LOW_RES=low_res
echo "Resizing such that longest side is $SIDE px long"
mkdir -p $LOW_RES
echo "- Resizing images"
for img in *.jpg; do
echo " -- Converting $img"
convert -resize ${SIDE}x${SIDE} "$img" ./${LOW_RES}/small_"$img"
done
if [ -d /Applications/ImageOptim.app ]
then
echo "- Opening ImageOptim to optimize resized images"
open -a ImageOptim ./${LOW_RES}/*.jpg
fi
Let's break it down:
SIDE=${1:-1280}
First, we parse the optional argument SIDE
which is the number of pixels to which we constrain the resized images. This means that the longest side of all resized images will be equally long. The syntax {1:-1280}
means that if no argument is passed, the default value 1280 is used.
mkdir -p $LOW_RES
Then, we create the output dir. The -p
tells mkdir
to only create the dir if it doesn't exits.
convert -resize ${SIDE}x${SIDE} "$img" ./${LOW_RES}/small_"$img"
We resize each jpeg using the convert
command of ImageMagic, prefix each copy with "small_", and save each copy in the created output dir. The syntax ${SIDE}x${SIDE}
(e.g. 1280x1280
) tells convert
to constrain the larger of the width and the height to $SIDE
, while maintaining the aspect ratio of the image.
if [ -d /Applications/ImageOptim.app ]
Check whether the application ImageOptim is installed or not. Note the use of -d
(directory) instead of -e
(file): In macOS, applications (.app
) are bundles of files packaged in a directory given a special .app
extension which makes it appear as a file.
open -a ImageOptim ./${LOW_RES}/*.jpg
Following instructions from ImageOptim, open an ImageOptim instance and optimize all resized images to save even more disk space. Note that this line is only executed if ImageOptim is installed.
Result
Recall the file structure presented at the beginning of this post:
/My_photo_session
DSC_6494.NEF
DSC_6466.NEF
DSC_6547.NEF
/developed_jpg
DSC_6494.jpg
DSC 6466.jpg
DSC_6547.jpg
Executing the script from /developed_jpg
results in the following file structure:
/My_photo_session
DSC_6494.NEF
DSC_6466.NEF
DSC_6547.NEF
/developed_jpg
DSC_6494.jpg
DSC 6466.jpg
DSC_6547.jpg
/low_res
small_DSC_6494.jpg
small_DSC 6466.jpg
small_DSC_6547.jpg
and the following file sizes:
DSC_6494.NEF // 29.1 MB
DSC_6494.jpg // 7.1 MB
small_DSC_6494.jpg // 140 kB