Skip to content

Instantly share code, notes, and snippets.

@mpaquette
Created November 3, 2020 18:20
Show Gist options
  • Save mpaquette/3fe5bd0677663d7b0a322740611c96e5 to your computer and use it in GitHub Desktop.
Save mpaquette/3fe5bd0677663d7b0a322740611c96e5 to your computer and use it in GitHub Desktop.
Crop and convert a bunch PNG into a merged PDF (riping slides from screenshots of zoom call for example)
echo "usage: png2pdfslides.sh FOLDER_WITH_PNG CROP_WIDTH by CROP_HEIGHT CROP_OFFSETX CROP_OFFSETX"
echo "output: merged.pdf in FOLDER_WITH_PNG"
echo " "
# This script converts PNG screenshots into a compiled PDF using file name ordering (presumably corresponding to timestamp if consecutive screenshots)
# 1) Goes into folder $MAINF
# 2) Crops an $WIDTH by $HEIGHT region for each PNG starting with a $OFFSETX pixels offset from the left and a OFFSETY pixels offset from the top
# 3) Converts each croped PNG to JPEG2000 for lossless PDF embedding
# 4) Converts each JPEG2000 to PDF
# 5) Merges all the PDFs together into one merged.pdf
MAINF=$1
WIDTH=$2
HEIGHT=$3
OFFSETX=$4
OFFSETY=$5
cd $MAINF
# temporary working folders
mkdir -p crop
mkdir -p jpeg2000
mkdir -p pdfs
# assumes pre-cleaned screenshot where naming convention fits time
# can handle non dense numbering since we never explicitely use counter after initial naming
# init counter
IDX=1
# loop over file using natural timestamp as ordering
# increment $IDX
echo " "
echo "CROPPING"
for f in *.png;
do
echo $f;
convert -crop "$WIDTH"x"$HEIGHT"+"$OFFSETX"+"$OFFSETX" "$f" "crop/crop_"$IDX".png";
ls "crop/crop_"$IDX".png"
IDX=$((IDX+1));
done
# loop over files using normal numbers (no need for leading zeros with -v)
# cut base path
# cut extension
# convert image to jpeg2000
# losslessly imbed jp2 in pdf
echo " "
echo "CONVERTING"
for f in $(ls -v crop);
do
s=${f##*/};
s=${s%.png};
convert "crop/$f" -quality 0 "jpeg2000/$s.jp2";
ls "jpeg2000/$s.jp2";
img2pdf -o "pdfs/$s.pdf" "jpeg2000/$s.jp2";
ls "pdfs/$s.pdf";
done
# merge into one pdf with true numerical ordering
echo " "
echo "MERGING"
cd pdfs
pdfunite $(ls -v) ../merged.pdf
cd $MAINF
ls merged.pdf
echo " "
echo "CLEANING PDFs"
rm -r pdfs
echo " "
echo "CLEANING JPEG2000s"
rm -r jpeg2000
echo " "
echo "CLEANING crops"
rm -r crop
@mpaquette
Copy link
Author

The easiest way to figure out the Width, height and offsets is to open one of the screenshot into GIMP, get a rectangle selection that make sense and rip the values, GIMP has the same convention as the convert tool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment