Create a gist now

Instantly share code, notes, and snippets.

@unot /zip2cbz.sh
Last active Feb 18, 2016

What would you like to do?
Zipped jpeg files to CBZ file for Kobo Glo.
#!/bin/bash -ex
# zip2cbz.sh
# Written by Takashi UNO
# require: zip, ImageMagick
if [ $# -ne 1 ]; then
echo "USAGE: `basename $0` hogehoge.zip"
exit 1
fi
for cmd in zip unzip convert ; do
if [ ! -x "`which $cmd`" ]; then
echo "command $cmd is not found."
exit 1
fi
done
if [ ! -f "$1" ]; then
echo "$1: file not exist."
exit 1
fi
TMPDIR="/var/tmp/`basename "$1" .zip`"
# TMPDIR="${1%.zip}"
trap 'rm -rf "${TMPDIR}"' EXIT
mkdir -p "${TMPDIR}/unzip"
mkdir "${TMPDIR}/png"
unzip -jq "$1" -d "${TMPDIR}/unzip"
OPTIPNGOPTIONS="-quiet"
SIZE="758x1024"
pushd "${TMPDIR}/unzip" >/dev/null
if [ ! -x `which parallel` ]; then
for src in *.jpg ; do
convert "$src" \
-type grayscale \
# -sharpen 5 \
-fuzz 50% -trim \
-linear-stretch 2%x2% \
-bordercolor white -border 2%x2% \
png:- | convert - -depth 4 -resize ${SIZE} ../png/"${src%.jpg}.png"
test -x `which optipng` && optipng ${OPTIPNGOPTIONS} ../png/"${src%.jpg}.png"
done
else
ls *.jpg | parallel -j4 "convert {} \
-type grayscale \
-fuzz 50% -trim \
-linear-stretch 2%x2% \
-bordercolor white -border 2%x2% \
png:- | convert - -depth 4 -resize $SIZE ../png/{.}.png"
test -x `which optipng` && optipng ${OPTIPNGOPTIONS} ../png/*.png
fi
popd >/dev/null
zip -jqr "${1%.zip}.cbz" "${TMPDIR}/png"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment