Created
June 4, 2023 10:18
-
-
Save mrnejc/2398e4f027df96446e221805c4aaf054 to your computer and use it in GitHub Desktop.
Convert PDF to CBZ archives via GhostScript; run with --help for some help
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 | |
# take care of spaces in filenames | |
SAVEIFS="$IFS" | |
IFS=$(echo -en "\n\b") | |
# requires ghostscript, ionice, zip, jhead & imagemagick/graphicsmagick | |
# do not delete temp directory with temp files | |
DELETE=false | |
# do not up/down-scale images | |
UPDOWNSCALE=false | |
CONVERTSCALE="100" | |
# default resolution is 150 dpi (*2 because we create JPGs at 50%) | |
PDF_RES=150 | |
PDF_RES_ORI=$PDF_RES | |
# default JPEG qulity is 80% | |
JPEGQ=80 | |
for param in "$@" | |
do | |
if [ "$param" = "--delete" ] || [ "$param" = "-d" ]; then | |
echo "config: will delete temporary files" | |
DELETE=true | |
elif [ "$param" = "--updownscale" ]; then | |
echo "config: will render to twice the size and rescale when creating final images" | |
UPDOWNSCALE=true | |
elif [ "$param" = "--scale" ]; then | |
# TODO get percentage (pop next?) | |
CONVERTSCALE="100" | |
elif [ "$param" = "--50dpi" ]; then | |
echo "config: render PDF to 50 dpi" | |
PDF_RES=50 | |
PDF_RES_ORI=$PDF_RES | |
elif [ "$param" = "--96dpi" ]; then | |
echo "config: render PDF to 96 dpi" | |
PDF_RES=96 | |
PDF_RES_ORI=$PDF_RES | |
elif [ "$param" = "--200dpi" ]; then | |
echo "config: render PDF to 200 dpi" | |
PDF_RES=200 | |
PDF_RES_ORI=$PDF_RES | |
elif [ "$param" = "--300dpi" ]; then | |
echo "config: render PDF to 300 dpi" | |
PDF_RES=300 | |
PDF_RES_ORI=$PDF_RES | |
elif [ "$param" = "--help" ]; then | |
echo "options [--delete] [--96dpi|--200dpi|--300dpi] [--scale X]" | |
echo "" | |
echo " --delete" | |
echo " delete temporary directory after CBZ creation" | |
echo " --50dpi|--96dpi|--200dpi|--300dpi" | |
echo " render PDF to 50/96/200/300 dpi images, if not set defaults to 150 dpi" | |
echo " --updownscale" | |
echo " render to twice the default dpi and scale down when converting to JPGs" | |
echo " --scale <PERCENT>" | |
echo " scale PDF rendered PNGs to PERCENT % when converting to JPGs; not compatible with --updownscale" | |
exit 0 | |
fi | |
done | |
# are we doing up/down-scaling ? | |
if [ "$UPDOWNSCALE" = "true" ]; then | |
PDF_RES=$(echo "$PDF_RES * 2" | bc) | |
CONVERTSCALE=$(echo "$CONVERTSCALE / 2" | bc) | |
fi | |
# for all PDFs | |
#for PDF in $(ls -1 *.pdf); | |
for PDF in "$@" | |
do | |
if [ ${PDF: -4} == ".pdf" ] | |
then | |
echo "Working on [$PDF]" | |
# get filename without extension | |
FILEP=${PDF%.*} | |
# clean filename without spaces | |
FILEPC=`echo $FILEP | sed 's| |\.|g'` | |
mkdir $FILEPC | |
# -dGraphicsAlphaBits=1 | |
echo " - [1/3] Extracting pages from [$PDF]" | |
gs -q -dNOPAUSE -dBATCH \ | |
-sDEVICE=png16m \ | |
-r$PDF_RES \ | |
-dTextAlphaBits=4 \ | |
-dUseCropBox \ | |
-sOutputFile=$FILEPC/%03d.png $PDF | |
echo " - [2/3] Converting pages to JPG, quality is $JPEGQ %" | |
for I in $(ls -1 $FILEPC/*.png) | |
do | |
convert $I -scale $CONVERTSCALE% -quality $JPEGQ ${I%%.png}.jpg && rm $I | |
done | |
jhead -purejpg -q $FILEPC/*.jpg | |
CBZ=$FILEP.$PDF_RES_ORI\dpi.cbz | |
echo " - [3/3] Packing to CBZ file [$CBZ]" | |
ionice -c 3 find "$FILEPC/" -type f | sort | zip -j -T -9 "$CBZ" -@ > /dev/null | |
if [ "$DELETE" = "true" ]; then | |
rm -rf $FILEPC | |
fi | |
echo "" | |
fi | |
done | |
IFS="$SAVEIFS" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment