Last active
April 13, 2020 09:39
-
-
Save gquittet/da3f4bfb3b9eb022f73060de03de06c7 to your computer and use it in GitHub Desktop.
Modify and convert all images from folders to one PDF
This file contains 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
#!/usr/bin/env sh | |
EXTENSION=".png" | |
OUTPUT_EXTENSION=".jpg" | |
function resize_image { | |
local FILE=$1 | |
local OUTPUT_FILE=${FILE/$EXTENSION/} | |
# Recto | |
convert "$FILE" -quality 90 -crop 1278x1920+400+90 +repage -resize 2480x3508\! "$OUTPUT_FILE"_recto"$OUTPUT_EXTENSION" | |
# Verso | |
convert "$FILE" -quality 90 -crop 1278x1920+1681+90 +repage -resize 2480x3508\! "$OUTPUT_FILE"_verso"$OUTPUT_EXTENSION" | |
} | |
JOBS=0 | |
MAX_JOBS=35 | |
BEGINNING_TIME=`date +%s` | |
for dir in */ | |
do | |
directory=${dir::-1} | |
echo "[INFO] Starting $directory..." | |
START_TIME=`date +%s` | |
cd "$directory" | |
for file in *.png | |
do | |
((JOBS=JOBS%MAX_JOBS)); ((JOBS++==0)) && wait | |
[ -f "$file" ] && resize_image "$file" & | |
done | |
wait | |
END_TIME=`date +%s` | |
echo "[INFO] Finished in $(($END_TIME-$START_TIME)) seconds." | |
echo "[INFO] Convert all images to one PDF..." | |
START_TIME=`date +%s` | |
# Don't use convert because it slows! | |
img2pdf --pagesize A4 -o "$directory".pdf *.jpg | |
mv "$directory".pdf pdfToTrim.pdf | |
pdftk pdfToTrim.pdf cat 2-end output "$directory".pdf | |
rm pdfToTrim.pdf | |
END_TIME=`date +%s` | |
echo "[INFO] PDF generated in $(($END_TIME-$START_TIME)) seconds." | |
cd .. | |
done | |
FINISHING_TIME=`date +%s` | |
echo "[INFO] FINISHED in $(($FINISHING_TIME-$BEGINNING_TIME)) seconds." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment