1

I need process scanned book images and remove the gray background. I want to find a correct -level operator values for the Imagemagick script. To do this, I need to make a series of tests and compare the results. like this:

convert file.tif -normalize -level 10%,90% outfile1.tif
convert file.tif -normalize -level 15%,75% outfile2.tif
convert file.tif -normalize -level 27%,76% outfile3.tif

and so on. Let's say 10 tests with -level values starting from 10% and adding a 2% step to increase: 10%, 12%, 14% and so on. The same for other value, only with step 5%: 70%, 75%, 80%,85%, 90%. How to make bash script that will run this tests and create single PDF file containg all these tests on the output? Then I will compare the results and choose the best one.

3
  • 2
    by the way, when doing something like that manually, don't go for "constant increments until I hit the spot"; do binary search, i.e. always query the middle of the interval that the right value might be in (for example: you always start with "somewhere between 0 and 100%", so first choice is 50%, say that's too dark, so you're then left with values between 50% and 100%, so you pick 75%, that's too bright so you then pick 62.5%, and so on). That's on average significantly quicker. Commented Jul 25, 2023 at 21:21
  • 2
    and, also, by the way, this really sounds like you'll be ending up with a small shell program to generate that PDF. Why not instead use these programming skills to automatically detect the right whiteness of shade of pale? (with all due apologies to Procul Harum) Commented Jul 25, 2023 at 21:24
  • Sharing a representative image would help. Commented Jun 5, 2024 at 12:48

1 Answer 1

1

There's more than one adaptive method for automatically thresholding black text printed on varying shades of white paper.

Consider using one of the three methods described by the docs:

-auto-threshold method

Automatically perform image thresholding.

Here are the valid methods: ...

  • Kapur ...
  • OTSU ...
  • Triangle ...
4
  • -auto-threshold is only in IM 7. I using IM 6.9 Commented Jul 26, 2023 at 11:00
  • 1
    The OP didn't describe a version constraint nor a reason for imposing it. Stop using downrev software already, as it is needlessly complicating matters for both you and collaborators you are trying to enlist. It's a free download, e.g. on MacOS the one-liner $ brew install imagemagick will put it in /usr/local/Cellar/imagemagick/7.1.1-12 Commented Jul 26, 2023 at 14:30
  • This does not seem to answer the question, which is "How to make bash script that will run this tests and create single PDF file containg all these tests on the output?" Commented Sep 3, 2023 at 8:09
  • @Kusalananda, yeah, I un-asked the question. I observed that there are known Automatic methods implemented in OP's favorite tool. A simple update would obviate the need for them or myself to spend time coding / testing / evaluating on (missing from OP) example images via some ill-specified Manual method. Even if there were a hard requirement to choose a single threshold applied to whole page image I would still use Otsu for local legibility, and compute average threshold employed by that Otsu image. Having asked wrong question for business needs, I hoped to nudge OP down a more productive path Commented Sep 3, 2023 at 16:27

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.