It is currently 20 Jun 2021, 06:31

• Featuring Members' Works (Image Attachments) •
1: member
2: member
3: member
4: member
5: member

GIMP/GL:  Tutorials | Tips | Questions | Interests | Script Requests
Recommendations:  Index | Top 10
Resources:  Python-Fu Plug-ins | 🡆 Scheme-Fu Scripts | GIMP
Python:  Lessons | Questions | Exercises
The Arts:  GIMPed | GIMPed Photos | Other | Contests | Public Domain Images | Isolated Images | Blender
Inkscape
Social:  Say Hello | Anything Goes | Shares | FCSP

Scan Text optimizer by .bat file for all files in a folder (resize, whitening, indexed, contrast)

Share your Scheme Fu scripts for GIMP here
Enndee
Newly Registered

Scan Text optimizer by .bat file for all files in a folder (resize, whitening, indexed, contrast)

Post#1 by Enndee » 28 May 2021, 19:34


Hello everybody,

i spent quite a lot of work with this and thats why I want to share my result with you. Maybe someone can make use of it. :)

Whats the purpose of the script:
I recently had to scan quite a lot of text pages (contracts and so on) to archive them.
Since i was never really satisfied with the presets my Scanner-Software offers to optimize scanned pages (color curves and "anti shine through" and so on) i tried different ways to make a batch process to optimize my scans but nothing worked as good as the manual work within gimp. Then i discovered this nice scheme feature and it's usability by .bat files and wanted to make use of it.

What i wanted to achieve exactly:
I wanted the background to be turned white when it's above a specific white-value (e.g. above (220,220,220)).
I wanted to resize the picture to a variable dimension
I wanted to convert the picture to indexed (because this saves a ton of space in PNGs).
I wanted to turn up the contrast a little because this usually gets a little lost after resizing a picture


The result I achieved here is able to shrink a text page of about 4 mb to a size of about 150 kb with almost no visual impact.
Batchresult.png
Batchresult.png # (61.65 KiB) Viewed 812 times (Thumbnail | Recognize | Sq Tile | LNFI)

I wanted all of the above features to be used on the files within the folder that I put the batch file into. So basically I just copy a shortcut to the batch file to the place where my source pictures lie and run that shortcut. After the batch process is finished, a set of pictures with the prefix "opti_" is being saved.
Important: in the shortcut you have to delete the value for "run in .." like in this picture (I just have a german version as example)
shortcut.png
shortcut.png # (20.17 KiB) Viewed 812 times (Thumbnail | Recognize | Sq Tile | LNFI)

What this script does and how you can use it:
This script sends the current path to GIMP to make use of the file-glob feature.
With file-glob it batches through all files doing the following worksteps:
  • check if file is already indexed and convert to RGB if so
  • selective gauss to delete jpeg artifacts
  • select all white-ish areas and set them plain white
  • resize the picture to a specific dimension
  • select all dark-ish areas and turn up the contrast after resizing
  • convert the picture to indexed
  • save picture with "opti_" in front of the filename

And this is the content of the .bat file that makes this possible:

Code: Select all

@echo V2.2
@echo off


rem: NIEMALS Leerstellen an den Gleichheitszeichen haben, sonst sind die Variablen leer

set gimp="C:\Program Files\GIMP 2\bin\gimp-console-2.10.exe"
set /A targetheight=2500
set /A colorpalettesize=15
set /A whitetheshold=25
set fileextension="*.png"
set fileextension2="*.jpg"




%gimp% --no-data -i -b "(let* ((thefiles (cadr (file-glob \"%fileextension%\" 0))))   (while (not (null? thefiles))		  (let*((thefilename (car thefiles))(image (car (gimp-file-load RUN-NONINTERACTIVE thefilename thefilename))) (drawable (car (gimp-image-get-active-layer image))) (dateiname-neu (string-append \"opti_\" (car(gimp-image-get-name image))))    (dateiname-neu (string-append (unbreakupstr (reverse (cdr (reverse (strbreakup dateiname-neu \".\")))) \".\") \".png\"))  	)	(gimp-message (car(gimp-image-get-name image))) (gimp-image-undo-disable image)                  (if (equal? (car (gimp-drawable-is-indexed drawable)) 1)(begin(gimp-image-convert-rgb image)(gimp-message \"Bild mit indizierter Palette erkannt\")))                    (plug-in-sel-gauss 1 image drawable 5 5)  (gimp-by-color-select-full drawable '(255 255 255) %whitetheshold% 2 1 0 1 0 0 1 0) (gimp-edit-fill drawable 2)  (if (> (car (gimp-image-height image)) (* %targetheight% 1.3)) 	(begin (gimp-image-scale-full image (* (/ %targetheight% (car (gimp-image-height image))) (car(gimp-image-width image))) %targetheight% 4) (gimp-by-color-select-full drawable '(0 0 0) 50 2 1 0 1 0 0 1 0)(gimp-selection-grow image 1)(gimp-drawable-brightness-contrast drawable -0.4 0.3)(gimp-drawable-brightness-contrast drawable -0.4 0.3) ))     (gimp-convert-indexed image NO-DITHER  MAKE-PALETTE %colorpalettesize% 0 1 \"\") (gimp-image-undo-enable image)     (file-png-save RUN-NONINTERACTIVE image drawable dateiname-neu \"\" 0 9 0 0 0 0 0)			(gimp-image-delete image))(set! thefiles (cdr thefiles)))	)"                     										-b "(let* ((thefiles (cadr (file-glob \"%fileextension2%\" 0))))   (while (not (null? thefiles))		  (let*((thefilename (car thefiles))(image (car (gimp-file-load RUN-NONINTERACTIVE thefilename thefilename))) (drawable (car (gimp-image-get-active-layer image))) (dateiname-neu (string-append \"opti_\" (car(gimp-image-get-name image))))   (dateiname-neu (string-append (unbreakupstr (reverse (cdr (reverse (strbreakup dateiname-neu \".\")))) \".\") \".png\"))  	)	(gimp-message (car(gimp-image-get-name image))) (gimp-image-undo-disable image)                  (if (equal? (car (gimp-drawable-is-indexed drawable)) 1)(begin(gimp-image-convert-rgb image)(gimp-message \"Bild mit indizierter Palette erkannt\")))                     (plug-in-sel-gauss 1 image drawable 5 5)      (gimp-by-color-select-full drawable '(255 255 255) %whitetheshold% 2 1 0 1 0 0 1 0) (gimp-edit-fill drawable 2)                                   (if (> (car (gimp-image-height image)) (* %targetheight% 1.3)) 	(begin (gimp-image-scale-full image (* (/ %targetheight% (car (gimp-image-height image))) (car(gimp-image-width image))) %targetheight% 4) (gimp-by-color-select-full drawable '(0 0 0) 50 2 1 0 1 0 0 1 0)(gimp-selection-grow image 1)(gimp-drawable-brightness-contrast drawable -0.4 0.3)(gimp-drawable-brightness-contrast drawable -0.4 0.3)))     (gimp-convert-indexed image NO-DITHER  MAKE-PALETTE %colorpalettesize% 0 1 \"\") (gimp-image-undo-enable image)     (file-png-save RUN-NONINTERACTIVE image drawable dateiname-neu \"\" 0 9 0 0 0 0 0)			(gimp-image-delete image))(set! thefiles (cdr thefiles)))	)" 					-b '(gimp-quit 1)' 




GIMP Learn rocks...

tim
GL Blogger

Post#2 by tim » 06 Jun 2021, 13:51


Thanks for sharing.
Hopefully some people can make use of it in the future.
aka Tin

         


GIMP/GL:  Tutorials | Tips | Questions | Interests | Script Requests
Recommendations:  Index | Top 10
Resources:  Python-Fu Plug-ins | 🡆 Scheme-Fu Scripts | GIMP
Python:  Lessons | Questions | Exercises
The Arts:  GIMPed | GIMPed Photos | Other | Contests | Public Domain Images | Isolated Images | Blender
Inkscape
Social:  Say Hello | Anything Goes | Shares | FCSP

Post Reply New Topic
  • Similar Topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 1 guest