Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.