#!/usr/bin/env python "compress all tiff files in subdirectories. Requires ImageMagick library" import os def testtif(file): #returns true if not compressed stdout_handle = os.popen('identify -verbose "' + file + '" | grep LZW', 'r') text = stdout_handle.read() return len(text) == 0 def compress(file): compress='convert -compress lzw ' tempname='temp.tif' print 'compressing "' + file + '"' os.system(compress + '"' + file + '" ' + tempname) os.remove(file) os.rename(tempname, file) #walk through directory tree for root, dirs, files in os.walk('.'): for name in files: #print 'checking ' + os.path.join(root,name) if name.endswith('.tif'): if testtif(os.path.join(root,name)): compress(os.path.join(root,name)) else: print ' ...' + os.path.join(root,name) + ' already compressed!'