Skip to content

Commit

Permalink
Add an option to set the downsamplethreshold
Browse files Browse the repository at this point in the history
  • Loading branch information
compilingEntropy authored and aklomp committed Jan 22, 2025
1 parent 92a92f2 commit 971c661
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ sometimes further reduce the output size:
./shrinkpdf.sh -g -r 90 -o out.pdf in.pdf
```

Set the threshold at which an image would be downsampled with the `-t` flag.
The default of 1.5 means that images which are already less than 1.5x the
desired dpi will not be resized. (Using `-r 300` and `-t 1.5` would not resize
images unless they were > 300 * 1.5 dpi, or 450 dpi.) Use lower numbers for
less leniency and higher numbers for more leniency.
```sh
./shrinkpdf.sh -r 300 -t 1.1 -o out.pdf in.pdf
```

Due to limitations of shell option handling, options must always come before
the input file.

Expand Down
16 changes: 12 additions & 4 deletions shrinkpdf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# https://github.com/aklomp/shrinkpdf
# Licensed under the 3-clause BSD license:
#
# Copyright (c) 2014-2023, Alfred Klomp
# Copyright (c) 2014-2025, Alfred Klomp
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -52,10 +52,13 @@ shrink ()
-dAutoRotatePages=/None \
-dColorImageDownsampleType=/Bicubic \
-dColorImageResolution="$3" \
-dColorImageDownsampleThreshold="$5" \
-dGrayImageDownsampleType=/Bicubic \
-dGrayImageResolution="$3" \
-dGrayImageDownsampleThreshold="$5" \
-dMonoImageDownsampleType=/Subsample \
-dMonoImageResolution="$3" \
-dMonoImageDownsampleThreshold="$5" \
-sOutputFile="$2" \
${gray_params} \
"$1"
Expand Down Expand Up @@ -112,22 +115,24 @@ usage ()
echo "Reduces PDF filesize by lossy recompressing with Ghostscript."
echo "Not guaranteed to succeed, but usually works."
echo
echo "Usage: $1 [-g] [-h] [-o output] [-r res] infile"
echo "Usage: $1 [-g] [-h] [-o output] [-r res] [-t threshold] infile"
echo
echo "Options:"
echo " -g Enable grayscale conversion which can further reduce output size."
echo " -h Show this help text."
echo " -o Output file, default is standard output."
echo " -r Resolution in DPI, default is 72."
echo " -t Threshold multiplier for an image to qualify for downsampling, default is 1.5"
}

# Set default option values.
grayscale=""
ofile="-"
res="72"
threshold="1.5"

# Parse command line options.
while getopts ':hgo:r:' flag; do
while getopts ':hgo:r:t:' flag; do
case $flag in
h)
usage "$0"
Expand All @@ -142,6 +147,9 @@ while getopts ':hgo:r:' flag; do
r)
res="${OPTARG}"
;;
t)
threshold="${OPTARG}"
;;
\?)
echo "invalid option (use -h for help)"
exit 1
Expand All @@ -168,7 +176,7 @@ check_overwrite "$ifile" "$ofile" || exit $?
get_pdf_version "$ifile" || pdf_version="1.5"

# Shrink the PDF.
shrink "$ifile" "$ofile" "$res" "$pdf_version" || exit $?
shrink "$ifile" "$ofile" "$res" "$pdf_version" "$threshold" || exit $?

# Check that the output is actually smaller.
check_smaller "$ifile" "$ofile"

0 comments on commit 971c661

Please sign in to comment.