User Tools

Site Tools


doc:appunti:linux:gimp_batch

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
doc:appunti:linux:gimp_batch [2022/11/23 17:18] – created niccolodoc:appunti:linux:gimp_batch [2022/11/23 17:39] (current) – [Call the script from the command line] niccolo
Line 3: Line 3:
 I had a set of many scanned images from magazine pages, the sizes were varying and I wanted to apply the GIMP **erode filter** to enhance the text readability. Generally for image batch processing I use **Imagemagick** from the command line, but I was not able to reproduce the erode filter with Imagemagick, so I searched the recipe to do **batch processing** with the GIMP. I had a set of many scanned images from magazine pages, the sizes were varying and I wanted to apply the GIMP **erode filter** to enhance the text readability. Generally for image batch processing I use **Imagemagick** from the command line, but I was not able to reproduce the erode filter with Imagemagick, so I searched the recipe to do **batch processing** with the GIMP.
  
-===== The erode filter =====+===== The plug-in-erode filter =====
  
 You can apply the erode filter in GIMP from menu //Filters// => //Generic// => //Erode//. There is no dialog box to control the filter parameters, but it turned out that when you want to call the **plug-in-erode** function from a script you have to pass several parameters. You can apply the erode filter in GIMP from menu //Filters// => //Generic// => //Erode//. There is no dialog box to control the filter parameters, but it turned out that when you want to call the **plug-in-erode** function from a script you have to pass several parameters.
Line 20: Line 20:
  
 It is not simple to have an explanation of each paramter, so I proceeded by trials. It turned out that the wanted result is obtaiuned by passing //propagate-mode// = //O:white//. I set to zero the other parameters too. It is not simple to have an explanation of each paramter, so I proceeded by trials. It turned out that the wanted result is obtaiuned by passing //propagate-mode// = //O:white//. I set to zero the other parameters too.
 +
 +===== Creating a script to call plug-in-erode =====
 +
 +The next step was to create a script which will simply call the plug-in, this is the file that I saved as **simple-myerode.scm**:
 +
 +<file>
 +(define (simple-myerode
 +            filename
 +            propagate-mode
 +            propagating-channel
 +            propagating-rate
 +            direction-mask
 +            lower-limit
 +            upper-limit)
 +    (let* ((image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
 +           (drawable (car (gimp-image-get-active-layer image))))
 +    (plug-in-erode  RUN-NONINTERACTIVE
 +                    image drawable propagate-mode propagating-channel
 +                    propagating-rate direction-mask lower-limit upper-limit)
 +    (gimp-file-save RUN-NONINTERACTIVE image drawable filename filename)
 +    (gimp-image-delete image)))
 +</file>
 +
 +Copy the script into the **$HOME/.config/GIMP/2.10/scripts/** directory, no particular action is required to enable it.
 +
 +===== Call the script from the command line =====
 +
 +Now it is possibile to execute the GIMP in batch mode, using the **%%-b%%** option. Calling the custom function ''simple-myerode'' you can pass the paramteres:
 +
 +<code bash>
 +#!/bin/bash
 +gimp -i -b "(simple-myerode \"IMAGE.JPG\" 0 0 0 0 0 0)" -b '(gimp-quit 0)'
 +</code>
 +
 +===== Combining Imagemagick and GIMP =====
 +
 +The final recipe works as follow:
 +
 +  * Enlarge the image at 250% with Imagemagick. This proved to be the best choice to enhance the text readability via the erode filter. This is because the erode filter works by adding a single pixel around the dark parts.
 +  * Apply the GIMP erode filter.
 +  * Use Imagemagick to apply the **%%-unsharp%%** filter. Resize to the original size and crop/resize the image to a fixed size, even if the input image is slightly wider or narrower. Force the resolution to 200 dpi ignoring that of the original image.
 +
 +This is the full script, which **receives the filename** to be processed and saves the output file into the **./outdir/** directory:
 +
 +<code bash>
 +#!/bin/bash
 +
 +BASENAME="$(basename "$1")"
 +TMP="tmp.$$.png"
 +
 +convert "$1" -scale '250%' "$TMP"
 +cp -p simple-myerode.scm /home/niccolo/.config/GIMP/2.10/scripts/
 +gimp -i -b "(simple-myerode \"$TMP\" 0 0 0 0 0 0)" -b '(gimp-quit 0)'
 +convert "$TMP" -unsharp '0x3+1.0+0' \
 +    -scale 'x2000' -gravity center -crop '1400:2000' \
 +    -background white -extent '1400x2000' -density 200 -units PixelsPerInch \
 +    -quality 90 \
 +    "./outdir/$BASENAME"
 +rm "$TMP"
 +</code>
  
doc/appunti/linux/gimp_batch.1669220292.txt.gz · Last modified: 2022/11/23 17:18 by niccolo