Skip to content

Instantly share code, notes, and snippets.

@bitsgalore
Created April 9, 2015 11:25
Show Gist options
  • Save bitsgalore/0c269a6399d2dd9a5a2e to your computer and use it in GitHub Desktop.
Save bitsgalore/0c269a6399d2dd9a5a2e to your computer and use it in GitHub Desktop.
For each JP2 image in a directory, generate derived image that discards user-defined number of quality layers. Requires Aware j2kdriver tool.
#!/bin/bash
# Generate derived JP2s that discard user-specified number of quality layers from source JP2s
# Requires:
# - j2kdriver (Aware)
#
# 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 3 ] ; then
echo "Usage: extractlayers.sh sourceDirectory destinationDirectory layersdiscarded" >&2
exit 1
fi
if ! [ -d "$1" ] ; then
echo "directory must be a directory" >&2
exit 1
fi
# Source directory
sourceDirectory="$1"
# Destination directory
destDirectory="$2"
# Layers to discard
layersdiscarded="$3"
# Aware ql argument
ql=$((layersdiscarded+1))
# Create destination directory if it doesn't exist
if ! [ -d $destDirectory ] ; then
mkdir $destDirectory
fi
# Normalise directories to absolute paths
sourceDirectory=$(readlink -f $sourceDirectory)
destDirectory=$(readlink -f $destDirectory)
# **************
# MAIN PROCESSING LOOP
# **************
# 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' file ; do
fName="$file"
# Base name (strip away path)
fNameNoPath=$(basename "$fName")
bName="${fNameNoPath%.*}"
# Generate output file name
outName="$destDirectory"$"/$bName"_lr.jp2
# Run j2kdriver
j2kdriver -i "$fName" -t JP2 -ql $ql -o "$outName"
done < <(find $sourceDirectory -name '*.jp2' -type f -print0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment