#! /bin/bash # Save this script as FindBWcomics.sh # It can run on Windows if you have cygwin and ImageMagick installed # This script scans a directory for all the cbr/cbz files it can find # The for each file it extracts one image and determines if the image is in colors # If not, it makes a copy to the directory given as 2nd parameter of the script # I use this script to find B&W comic books for my eBook reader # This script doesn't change the files in the source directory # Note that there are many ways that this script can fail: # - if the pages alternate between color and B&W, results will be random # - if the archive contains other types of files # - if the rar archive contains several different directories # - if the inner pathname contains problematic characters such as [ ] { } ( ) / \ ' "... # Many conversions will fail for this last reason # (c) Guillaume Dargaud 2010 - www.gdargaud.net # Free use and distribution # History: 20100518 - 2nd version that can handle arbitrary file names # You can change the following 2 parameters export Limit=1000 # If fewer than those many colors, then we consider it BW. Default: 256 export Temp=/tmp # Temporary directory export DO=ln # Use ln or cp for a link or a copy if [ $# -ne 2 ]; then echo 1>&2 "Usage: $0 ScanDir DestDir" echo 1>&2 "Find all cbr/cbz comic book files and copy those in BW to DestDir" exit 127 fi SourceDir=$1 # Directory to scan export DestDir=$2 # Copy there if the file contains a BW imagemkdir $Temp/$$ # Temp storage # Make a copy of the cbr or cbz file passed as argument if it contains mostly BW images function CopyIfBW { shopt -s nocasematch function clean { rm -rf $Temp/$$; echo; exit; } mkdir -p $Temp/$$ # Temp storage # We don't trust the file extension, let's do a 'file' on it. Type=$( file -b "$1" ) Type=${Type:0:3} case $Type in Zip ) unzip -qq "$1" -d $Temp/$$ ;; RAR ) Source=$(readlink -f "$1") # Convert to absolute path pushd $Temp/$$ >/dev/null unrar e -ep -inul -c- "$Source" #unrar-free -x --extract-no-paths "$Source" popd >/dev/null # Important note: sometimes unrar hangs. I just issue "killall unrar" and the script carries on ;; * ) echo "ERROR: $1 is neither ZIP nor RAR but "$(file -b "$1") return; ;; esac chmod -R +w $Temp/$$ # Pic a random files Third=$(( $(find $Temp/$$ -type f | wc -l )/3+3 )) File="$( find $Temp/$$ -type f | sort | head -$Third | tail -1 )" # Try various methods to determine colors: Class=$(convert "$File" -format "%[colorspace]" info:) if [[ x$Class == xGray ]]; then $DO -vi "$1" "$DestDir/"; clean; fi # Also usable, but probably redundant: # convert logo: -format "%[colorspace]" info: # Gray # convert logo: -type grayscale -format "%[channels]" info: # gray # convert logo: -format "%r" info: # PseudoClassGray # Colors=$( convert "$( find $Temp/$$ -type f | sort | head -$Half | tail -1 )" -format "%k" info:- ) Colors=$(convert "$File" -format "%k" info:) #echo "$Colors colors" if [[ x$Colors == x ]]; then echo "ERROR: $Type/$IT \| $1 \| $Image"; clean; fi if (( $Colors <= $Limit )); then $DO -vi "$1" "$DestDir/"; clean; fi # We assume the file is in color: Nothing to do rm -rf $Temp/$$ } export -f CopyIfBW find $SourceDir -type f \( -iname \*.cbz -o -iname \*.cbr \) -exec bash -c 'CopyIfBW "{}"' \;