Created
December 14, 2017 15:02
-
-
Save dmaglio/8a339389a9f17b9df848aa8a6d86da5c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
OPTIPNGOPTIONS="-quiet" | |
SIZE="600x800" | |
CWD="$(pwd)" | |
for cmd in zip unzip unrar 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 | |
file=$1 | |
if [[ "$file" =~ \ |\' ]]; then | |
newname=${file//[^0-9A-Za-z_.]/_} | |
echo "newname $newname" | |
if [ ! -e "$newname" ]; then | |
cp "$file" "$newname" | |
fi | |
file=$newname | |
fi | |
echo "file: $file" | |
if [[ ${file: -3} == "cbz" ]]; then | |
mv "$file" "$(basename "$file" .cbz).zip" | |
file="$(basename "$file" .cbz).zip" | |
elif [[ ${file: -3} == "cbr" ]]; then | |
mv "$file" "$(basename "$file" .cbr).rar" | |
file="$(basename "$file" .cbr).rar" | |
fi | |
echo "file: $file" | |
if [[ ${file: -3} == "zip" ]]; then | |
TMPDIR="$CWD/tmp/$(basename "$file" .zip)" | |
elif [[ ${file: -3} == "rar" ]]; then | |
TMPDIR="$CWD/tmp/$(basename "$file" .rar)" | |
fi | |
echo "tmpdir ${TMPDIR}" | |
#trap 'rm -rf "${TMPDIR}"' EXIT | |
mkdir -p "${TMPDIR}/unzip" | |
mkdir "${TMPDIR}/png" | |
if [[ ${file: -3} == "zip" ]]; then | |
unzip -q "$file" -d "${TMPDIR}/unzip" | |
elif [[ ${file: -3} == "rar" ]]; then | |
unrar x "$file" "${TMPDIR}/unzip" | |
else | |
exit 3 | |
fi | |
pushd "${TMPDIR}/unzip" >/dev/null | |
SAVEIFS=IFS | |
IFS=$(echo -en "\n\b") | |
array=($(find . -depth -type d)) # Find catalogs recursively. | |
pattern=" |'" | |
for i in "${array[@]}"; do | |
pushd "$i" >/dev/null 2>&1 | |
for name in *; do | |
if [[ "$name" =~ $pattern ]]; then | |
newname_inside=${name//[^0-9A-Za-z_.]/_} | |
if [ ! -e "$newname_inside" ]; then | |
mv "$name" "$newname_inside" | |
fi | |
fi | |
done | |
popd >/dev/null 2>&1 | |
done | |
IFS=$SAVEIFS | |
SAVEIFS=IFS | |
IFS=$(echo -en "\n\b") | |
array=($(find . -type d -printf "%P\n")) # Find catalogs recursively. | |
for i in "${array[@]}"; do | |
mkdir -p "${TMPDIR}/png/$i" | |
done | |
IFS=$SAVEIFS | |
# if [ ! -x "$(which parallel)" ]; then | |
find . -name "*.jpg" -printf "%P\n" | while read -r src; do | |
echo "$src" | |
convert "$src" -type grayscale -fuzz 50% png:- | convert - -depth 4 -resize $SIZE "${TMPDIR}/png/${src%.jpg}.png" && optipng ${OPTIPNGOPTIONS} "${TMPDIR}/png/${src%.jpg}.png" | |
done | |
# else | |
# find . -name "*.jpg" -printf "%P\n" | while read -r src; do | |
# echo "$src" | |
# parallel -j4 "convert $src -type grayscale -fuzz 50% png:- | convert - -depth 4 -resize $SIZE ${TMPDIR}/png/${src%.jpg}.png && optipng ${OPTIPNGOPTIONS} ${TMPDIR}/png/${src%.jpg}.png" | |
# done | |
# fi | |
popd >/dev/null | |
if [[ ${file: -3} == "zip" ]]; then | |
zip -qr "${file%.zip}_kobo.cbz" "${TMPDIR}/png" | |
elif [[ ${file: -3} == "rar" ]]; then | |
zip -qr "${file%.rar}_kobo.cbz" "${TMPDIR}/png" | |
fi | |
echo "$file" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment