Free up space on an iPhone by compressing your photo album

Most people that have had iPhones for more than a year or two have accumulated a massive amount of photos, at least several gigabytes in size. With iPhones still only shipping with a puny 16 GiB of storage by default (unless you pony up the $100 for a huge jump to the 64 GiB model with the iPhone 6/6+), Apple has been making a lot of money off of people looking to keep their photos and still have room to take more. But there’s another option: you can optimize and compress your existing photos to make them take up less space, and recover free space for your use.

Using the right tools and depending on the nature of your photos and images, you’ll be able to recover anywhere from 5% to 20% of your photo album size with these instructions!

Depending on the whether or not your iPhone is jailbroken, you’ll be able to carry out the instructions in either one or two passes. Note that there are a lot of various utilities and tools used below, you’ll need to obtain them by downloading from the respective websites, building from source, or using a package manager (apt, brew, yum, pkg, etc).

Requirements:

If you have a jailbroken iPhone, you can connect directly to the iPhone and carry out the image optimization over ssh (via sshfs). For best performance and reliability, we’ll set up a TCP/IP tunnel over USB instead of connecting over the wi-fi, using itunnel:

./itnl --lport 9990 --iport 22 &

And then mount the iPhone over SSH:

mkdir /Volumes/iPhone
sshfs -p9990 -o reconnect -C root@localhost:/ /Volumes/iPhone/

And then CD into the photos directory:

cd /Volumes/iPhone/var/mobile/Media/DCIM/

If your iPhone isn’t jailbroken, what we’ll do instead is create a backup via iTunes, CD into the backup directory, then optimize the images on the local PC. Once that’s done, we can restore the backup to the iPhone with the new images.

Next, we’ll optimize the images in three steps:

find . -iname "*.png" | parallel -j 8 --verbose 'pngcrush -brute {} {}2 && mv {}2 {}' \;

This command uses pngcrush to optimize all PNG images, usually created by taking screenshots or saving images from the web. You can remove the -brute parameter to speed up the process significantly, at the cost of lower space savings.

Note: While pngcrush can read and write to the same file in a single pass (by specifying -ow to overwrite), this is not threadsafe as it will write to a temporary file of the same name for all threads! As such, we manually specify the new file name and then move it back to overwrite the original.

find . -iname "*.jpg" | parallel -j 8 --verbose jpegtran -copy none -optimize -outfile {} {} \;

This step uses jpegtran (preferably the mozjpeg version) to optimize all JPEG images, the default format used by the camera app on the iPhone. As jpegtran does not use a lot of CPU resources, we use 32 threads on an 8-core machine to completely saturate the USB link. You can experiment with the job count for best performance.

find . -iname "*.thm" | parallel -j 8 --verbose 'cjpeg -quality 75 -optimize {} > {}2 && mv {}2 {}' \; 

In this step, we recompress and optimize all thumbnails stored on the iPhone. Since thumbnails are lossy by default, we can use a lower quality version here. The degree of lossiness can be modified by changing the -quality parameter, but 75 is the most common value.

Optional: We can now re-run the above steps on /var/mobile/Library/SMS to compress PNG and JPEG attachments in messages (iMessage or SMS), to possibly save a lot more free space.

Leave a Reply

Your email address will not be published. Required fields are marked *