next scanning administrative documents, mostly black and white documents I receive via postal mail,
I try to process the scans with gimp to maximise their levels, so white is white and black black.
gimp menu: Colors > Levels... seeting levels then ok
the operation takes time and I would like finding a way to do autonatically via the cli
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To automate the adjustment of levels in your scanned documents using GIMP via the command line, you can use GIMP's batch processing capabilities with a Scheme script. Here's a step-by-step guide:
Step 1: Create a Shell Script
Create a shell script that loops over your input files, applies auto-levels using GIMP, and saves the processed files.
Shell Script Example (process_scans.sh):
bash
#!/bin/bash
# Loop over all input files (e.g., .jpg, .png, .tif)
for file in *.jpg; do
# Create output filename (e.g., input.jpg → input_processed.jpg)
output="${file%.jpg}_processed.jpg"
# Run GIMP in batch mode with a Scheme script
gimp -i -b "
(let* (
(image (car (gimp-file-load RUN-NONINTERACTIVE \"$file\" \"$file\")))
(drawable (car (gimp-image-get-active-layer image)))
)
(gimp-drawable-autolevels drawable) ; Apply auto-levels
(gimp-file-save RUN-NONINTERACTIVE image drawable \"$output\" \"$output\") ; Save output
(gimp-image-delete image) ; Clean up
(gimp-quit 0) ; Exit GIMP
)
"
done
Step 2: Make the Script Executable
bash
chmod +x process_scans.sh
Step 3: Run the Script
bash
./process_scans.sh
How It Works:
Loop Over Files: The script processes all .jpg files in the current directory (change the file extension if needed, e.g., .png).
Auto-Levels Adjustment: Uses gimp-drawable-autolevels to automatically expand the tonal range (whites → white, blacks → black).
Save Output: Saves the processed file with _processed appended to the filename.
Batch Mode: GIMP runs in headless mode (-i) and executes the Scheme script to handle each file.
Customizing the Script
Adjusting Levels Manually (Optional):
If you prefer to set specific levels instead of auto-levels, replace gimp-drawable-autolevels with gimp-drawable-levels and define parameters. For example:
Ensure the filename extension matches the desired format (e.g., .png).
Notes:
Dependencies: Ensure GIMP is installed and accessible via the command line (gimp --version should work).
File Types: Works best with image formats like JPEG, PNG, or TIFF. PDFs may require conversion first.
Testing: Test the script on a single file before processing all documents:
bash
./process_scans.sh input.jpg
This approach automates the tedious manual adjustment, saving time and ensuring consistency across your documents.
User
maybe if possible via image magick or other cli linux program
Assistant
Yes, you can achieve this more efficiently using ImageMagick, a powerful command-line tool for image processing. It’s lighter and faster than GIMP for batch tasks. Here's how to automate adjusting levels (or normalizing) your scans:
Using ImageMagick
Basic Command for Auto-Level Adjustment
bash
convert input.jpg -auto-level output.jpg
Batch Processing Script
Create a shell script to process all images in a directory:
bash
#!/bin/bash
# Define input and output directories (optional, for organization)
INPUT_DIR="input_scans"
OUTPUT_DIR="processed_scans"
# Create output directory if it doesn't exist
mkdir -p "$OUTPUT_DIR"
# Process all image files (e.g., .jpg, .png, .tif)
for file in "$INPUT_DIR"/*.[jJpP][pPnN][gGtTiI]*; do
filename=$(basename "$file")
output="$OUTPUT_DIR/${filename%.*}_processed.${filename##*.}"
# Apply auto-levels
convert "$file" -auto-level "$output"
done
How to Use:
Save the script as process_scans.sh.
Place your input files in the input_scans folder.
Run:
bash
chmod +x process_scans.sh
./process_scans.sh
Processed files will be saved in processed_scans/.
Customization Options
1. Normalize (Alternative to Auto-Level)
If -auto-level doesn’t give desired results, try normalization:
bash
convert input.jpg -normalize output.jpg
This stretches the image’s intensity values to the full dynamic range (0–255).