Skip to content

Instantly share code, notes, and snippets.

@bitsgalore
Created April 8, 2015 16:18
Show Gist options
  • Save bitsgalore/eeff2b7dd2e44a5f6195 to your computer and use it in GitHub Desktop.
Save bitsgalore/eeff2b7dd2e44a5f6195 to your computer and use it in GitHub Desktop.
Compute compression ratio for all JP2s in directory tree
#!/bin/bash
# Compute compression ratio of each JP2 in directory tree, report results to CSV file
# Requires:
# - jpylyzer
# - xmllint (part of libxml library)
#
# If you're using Windows you can run this shell script within a Cygwin terminal: http://www.cygwin.com/
#
# Installation directory
instDir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# **************
# USER I/O
# **************
# Check command line args
if [ "$#" -ne 1 ] ; then
echo "Usage: compratio.sh rootDirectory" >&2
exit 1
fi
if ! [ -d "$1" ] ; then
echo "directory must be a directory" >&2
exit 1
fi
# Root directory
rootDirectory="$1"
# **************
# MAIN PROCESSING LOOP
# **************
counter=0
# Select all files with extension .jp2.
# Now works for filenames that contain whitespace using code adapted from:
# http://stackoverflow.com/questions/7039130/bash-iterate-over-list-of-files-with-spaces/7039579#7039579
while IFS= read -d $'\0' -r file ; do
fName="$file"
counter=$((counter+1))
# Run jpylyzer
jpylyzer "$fName" > tmp.xml
# Extract compression ratio from jpylyzer output
cRatio=$(xmllint --xpath "//*[local-name()='jpylyzer']/*[local-name()='properties']/*[local-name()='compressionRatio']/text()" tmp.xml)
# Result to stdout
echo \"$fName\",$cRatio
done < <(find $rootDirectory -name '*.jp2' -type f -print0)
rm tmp.xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment