Reclaiming disk space from TIF files*

Okay. So it’s disk cleanup time. Over the years I’ve accumulated a fair amount of images, not all of them have been saved with compression – which vastly reduces their file size.

In my case I’m looking at TIF and SGI files. Many time Flame operators forget to click the ‘compression’ button when they save out these formats due mostly to ignorance. What they don’t realize is that it’s loss-less compression. I digress.

So let’s reclaim disk space! Here is one stab at it using a bash script. Requires imagemagick. This will recursively descend from the current working directory.

*this code could be modified to deal with SGI’s, or targa (TGA) files. Make sure to check that convert’s -compress option is valid for those types!

#!/bin/bash

tifs=$(mktemp)
utifs=$(mktemp)
utifsizes=$(mktemp)

echo "Finding TIF files... (Please wait) > $tifs"
find . -iname "*.tif" -exec du {} \; > $tifs

echo -n "Disk usage of tif files "
awk 'BEGIN { sum = 0 } {sum = sum + $1} END {print sum " MB"}' $tifs

echo "Finding uncompressed TIF files... (Please wait) > $utifs"
cut -f2- $tifs | while read line ; do
identify -quiet -format "image-compression %C %d/%f" "$line" | grep None >> $utifs
done

echo -n "Number of uncompressed TIF files... "
count=$(wc -l $utifs | cut -d' ' -f1)
echo $count

if [ "$count" -ne "0" ] ; then

echo -n "Computing disk space used by uncompressed TIF files... > $utifsizes"
cut -f2- $tifs | while read line ; do
du -m "$line" >> $utifsizes
done
awk 'BEGIN { sum = 0 } {sum = sum + $1} END {print sum " MB"}' $utifsizes

echo "Compressing TIF files... (Please wait) "
cut -d' ' -f3- $utifs | while read line ; do
echo convert -compress lzw \""$line"\" \""$line"\"
convert -quiet -compress lzw "$line" "$line"
done

fi