Ubuntu – How to batch-process images to adjust contrast in terminal

command lineimage processing

I've scanned 200+ pages. I am wondering if it is possible to touch these up by running a batch command line image software with a contrast manipulation option. Essentially I am wanting to give a command such as:

image_software *.JPG --contrast -1

The pages I scanned have content on both sides. The smallest amount of the content from the reverse side is showing in the scanned image. It is like a watermark — online annoying.

What software + command might work for what I am describing?

Best Answer

One excellent option is to use ImageMagick's -brightness-contrast option in combination with a bash for loop.

To see how it all works first find a test image and experiment with the following syntax:

convert -brightness-contrast 10x5 input.jpg output.jpg

The -brightness-contrast option has 2 elements:

  1. -brightness. In the example above this has been set at 10 and possible settings are from -100 to +100. Positive values increase the brightness while negative values decrease the brightness. Using a '0' value means that the brightness will remain unchanged.
  2. -contrast. In the example above this has been set at 5 and again possible settings are from -100 to +100. Positive values increase the contrast while negative values decrease the contrast. Using a '0' value means that the contrast will remain unchanged.

Once you have found the settings best for your image you can navigate to the folder holding your images and run a bash for loop:

for j in *.jpg
do 
  convert -brightness-contrast 10x5 "$j" altered_"$j"
done

Here you can see I have made a small naming alteration for the output file which you can of course tailor to your specific needs.

There are many different ways to accomplish your goal with ImageMagick but this would be my own choice as it is both the easiest to use and to understand :).

References: