Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to set DownsampleThreshold #21

Closed
wants to merge 2 commits into from

Conversation

compilingEntropy
Copy link
Contributor

@compilingEntropy compilingEntropy commented Jan 16, 2025

Added the ability to set the DownsampleThreshold using the -t flag. This is desirable because the default threshold is 1.5, which results in a high variance of image quality within a document and larger than necessary file sizes.

When ghostscript considers an image for downsampling, it checks the desired dpi against the actual dpi for an image. If the actual dpi is "bigger enough" than the desired dpi, the image is downsampled. What does "bigger enough" mean? That's configurable with an option called the DownsampleThreshold.
By default, ghostscript uses 1.5 as the downsample threshold. This means that if you desire the images to have a dpi of 300 and set that resolution (in this script using -r), images above 300*1.5 = 450 dpi will be downsampled down to 300. Images with 440 dpi will remain 440 dpi.
The result is a document which may have images of vastly differing appearance quality, and at some resolutions that can be a bit visually jarring. Moreover, without this setting if a user is targeting a specific file size they will continually reduce the resolution setting until that filesize is met, and the file that results would have perhaps the same average image quality (that is, the same average image quality as a file of equivalent size made using a lower threshold but higher dpi), but a much worse minimum image quality than a file made with a low DownsampleThreshold (but higher dpi) since the variance is going to be much higher.

Now, it wouldn't be worthwhile to convert an image with 310 dpi to 300 dpi because the size savings would be negligible and the image would be more than 10 dpi worse (any conversion naturally incurs a quality loss irrespective of dpi), which is why this feature exists to begin with. I simply make the case that this script would benefit from exposing that threshold so that users can easily modify it. A more reasonable threshold than the default of 1.5 is probably closer to 1.1 or so in my testing, but I've left my opinion out of this commit.

The default value for -t is set to 1.5 as specified by the documentation, so by default no behavior is changed. Read more about this feature on this page (search DownsampleThreshold).

In the end I'm not sure I've explained myself well so please ask questions if I wasn't clear.

@aklomp
Copy link
Owner

aklomp commented Jan 19, 2025

Normally I'm conservative about adding new features to this script, but this one seems like it belongs. Well done on a very nice PR that includes an update of the README (easily forgotten) and a really well written PR description. Will merge this.

Apart from the question I asked on #20 about attribution, would you mind if I squash the two commits during the rebase/merge? I prefer to not go back and forth on spacing issues but condense the feature into a single commit.

@compilingEntropy
Copy link
Contributor Author

compilingEntropy commented Jan 20, 2025

Thanks for the approval, I find value in this feature and I'm not keen on maintaining my own fork so I appreciate it. Go ahead and merge whatever way is easiest for you.

As with my other PR, I waive all rights to the code contained in this PR and no attribution is necessary.

@aklomp
Copy link
Owner

aklomp commented Jan 22, 2025

Merged, thanks!

@aklomp aklomp closed this Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants