Skip to content

Instantly share code, notes, and snippets.

@stefancrain
Created February 14, 2015 01:16
Show Gist options
  • Save stefancrain/da6a07b234c4bc013dfd to your computer and use it in GitHub Desktop.
Save stefancrain/da6a07b234c4bc013dfd to your computer and use it in GitHub Desktop.
Find all image files and convert them to a jpgs with ImageMagick's convert
#!/bin/bash
# 2015 Stefan Crain - [email protected]
# Find all image files and convert them to a jpgs with ImageMagick's convert
#
# Usage
# ./slowconvert.sh toscan/ output/
parsedCLI=("$@")
scandir=${parsedCLI[0]%/}
outputdir=${parsedCLI[1]:-$scandir}
outputdir=${outputdir%/}
threads=${parsedCLI[2]:-`sysctl -n hw.ncpu`}
if [ -z "$scandir" ]; then printf "\033[35;1mIncorrect Usage,Try\n\033[0m./convert.sh /toscan/ /output/" && exit 1; fi
echo "Processing images from '"$scandir"' to '"$outputdir"' "$threads" at a time"
mkdir -p "$outputdir"
cd "$scandir"
GLOBAL_start_time=`date +%s`
file * | grep " image data" | cut -d: -f1 | # find files with image data within folder
sed "s#\(.*\)#\\1 $outputdir/\\1#g" | # modify string to repeat filename and include $outputdir
sed "s/\.[^.]*$/.jpg/g" | # replace final extension with jpg
xargs -P$threads -L1 -n2 convert > /dev/null 2>&1 # use all cores > don't display output
echo -e "\033[35;1mCreating jpg files took: "`expr $(date +%s) - $GLOBAL_start_time`" Seconds\033[0m"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment