We live in an age of exceptionally cheap storage. Right now, I can buy
an external hard-disk 2TB in size for £70. As such, storing a large
amount of almost any kind of file isn’t really an issue.
In light of that fact, running any sort of optimisation that results in
savings of less than one percent of your total hard-disk size might seem
unnecessary. It probably is unnecessary, but having grown up with
computers where the hard-disks were initially measured in the tens of
megabytes, I haven’t quite been able to shake the feeling that every
byte that can be saved should be saved. (It’s likely there’s also a
certain element of technical vanity involved too.)
One of the ‘easy’ targets of this optimising impulse was my iPhoto
library. Using jpegoptim, it’s possible to reduce the file-size of
images without any loss in quality.
My iPhoto library started 14.3GB in size,1 and seemed like it could
benefit from a little optimisation. It also seemed easier than trying
to sort through them. As with
all endeavours like this, it also offered a great opportunity to learn a
bit more about using a cornerstone *nix tool — in this case, find.
How to Do It
Before You Even Think About Starting
The optimisation process itself is lossless — file-size savings come
from re-organising the Huffman tables within the images rather than
reducing their quality, but you need to make sure you have at least
one known-good backup of your iPhoto/Aperture library before proceeding.
This command shouldn’t result in any data loss, but it does
overwrite the original images with the new, optimised copies, so there
is the potential for data loss.
So, before going any further, make sure you have at least one backup
because I accept absolutely no responsibility if this backfires
horribly and wipes out your photo library/ies. You do this at your own
risk.
Also, make sure you don’t have iPhoto or Aperture running when you carry
out the optimisation. It probably won’t cause any problems, but I’m not
sure, and it seems sensible not to have it accessing the photos as
you’re attempting to optimise them.
Pre-requisites
Now that you’ve got a known-good backup in place, you’ll need the
following two tools:
Where You Need to Be
To optimise your iPhoto library — and only your iPhoto library — you
need to be inside its bundle. (If you run with from the root of your
home directory, you’ll optimise every image it comes across in there.)
Assuming you have your library in the default location, this is really
straight-forward: just cd into it, like so:
cd ~/Pictures/iPhoto\ Library/
(For your Aperture library, just type Aperture instead of iPhoto.
If you wanted to carry out the same optimisation on photos in your
Dropbox, cd into it and run the command — it’s designed to be
‘location agnostic’.)
The Command Itself
Now that you’re in your iPhoto library, simply type the following
command into your shell and hit enter:
find . -iname '*.jp*' -exec jpegoptim -opv {} \;
The command contains several parts:
find . -iname '*.jp*'
This part invokes find to recursively locate files within the
current directory (and its subdirectories) of any name (the first
‘*’) with extensions beginning ‘jp’, which means find will
pick up both .jpg and .jpeg files.2
-exec
This simply tells find to pass any matching files to the following
command.
jpegoptim -opv {} \;
This invokes jpegoptim to optimise the images. The following
flags (o, p, and v) tell jpegoptim to carry out the
following actions on the images it’s processing:
o - overwrite original image with the new, optimised image;
p - preserves file modification and creation dates;
v - verbose, so reports after each file is optimised, and by
how much.3
The trailing ‘{} \;’ is required by the find command to operate
correctly on the images being passed to jpegoptim: {} is
replaced by the filename, and the \; is the escaped terminating
; character, required to tell find there are no more commands to
be executed.
The command itself can take anything for several seconds to several
hours to run, depending how many photos it finds. You can keep working
on other things whilst it does it, though your Mac might get a little
warm on the bottom as it’s working, and your hard-disk will be active
almost constantly.
The End Results
My iPhoto library:
- Size before: 14.30GB
- Size after: 13.69GB
- Size saving: 0.61GB
- Percentage saving: 4.27%
A friend of mine is a much more enthusiastic
photographer than I; the combined size of his iPhoto and Aperture
libraries was 41.72GB without any optimisation.4
The results of his optimisation:
- Size before: 41.72GB
- Size after: 40.70GB
- Size saving: 1.02GB
- Percentage saving: 2.44%
Conclusions
I can’t explain why Iain’s optimisation, on a percentage basis, was less
successful than mine. The best idea I have is that some cameras produce
more optimised photos than others; Iain’s photos were taken with better
cameras than mine, so it’s possible that higher-end equipment does a
better job of optimising the photos on the fly.
Purely in terms of space saved, it probably isn’t worth it unless you’re
on a relatively small SSD where each gigabyte saved represents a more
substantial percentage of your total storage. If, like me, you’re
constantly battling to find more space, then it also becomes worth it,
especially if you have a truly massive library. And the final benefit
is that it reduces the upload time and space used if you backup or sync
your library to your Dropbox, where space is definitely at a premium.