Last active
November 11, 2016 12:45
-
-
Save yawo/cdf5236ae40291b1ec575775f6c2849d to your computer and use it in GitHub Desktop.
Gimp script for whitening the background of all images in a folder. support jpg but can be adapted
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (drop-ext str) | |
(unbreakupstr (butlast (strbreakup str ".")) ".") | |
) | |
; extract basename without ext | |
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= | |
( define ( script-fu-whitenbackground | |
sourceDirectory targetDirectory ) | |
( let* | |
( | |
; Declare and Init local variables | |
( returnVal #f ) | |
; Guess host OS based on directory path separator | |
( isLinux ( > | |
( length ( strbreakup sourceDirectory "/" ) ) | |
( length ( strbreakup sourceDirectory "\\" ) ) ) ) | |
; Form path/file patternSource based on OS | |
( patternSource ( if isLinux | |
( string-append sourceDirectory "/*.[jJ][pP][gG]" ) | |
( string-append sourceDirectory "\\*.[jJ][pP][gG]" ) ) ) | |
( patternTarget ( if isLinux | |
( string-append targetDirectory "/*.[jJ][pP][gG]" ) | |
( string-append targetDirectory "\\*.[jJ][pP][gG]" ) ) ) | |
; List of files to be converted formatted for current Host | |
; O/S | |
( filelistSource ( cadr ( file-glob patternSource 1 ) ) ) | |
( filelistExists ( cadr ( file-glob patternTarget 1 ) ) ) | |
( checkFileExists filelistExists ) | |
; Place holders for image variables - updated per image | |
( theImage 0 ) | |
( theOrigLayer 0 ) | |
( theBgLayer 0 ) | |
( theDrawable 0 ) | |
( currentFile "" ) | |
( baseName "" ) | |
( outFilename "" ) | |
; Constants used to assign values to parasites on new image | |
( doIt #t ) | |
( checkFile "" ) | |
) ; End declaration of Local Variables | |
; | |
; Run if images closed, message if not. | |
( if ( < 0 ( car ( gimp-image-list ) ) ) | |
( gimp-message "Close open Images & Rerun" ) | |
( begin | |
; | |
; Run within scope of let* and local variables | |
; 'baseName' is filename without .jpg extension | |
; 'outFilename' is filename with .xcf extension | |
; Step through each file in list with while loop. | |
( while ( not ( null? filelistSource ) ) | |
( set! doIt #t ) | |
( set! checkFileExists filelistExists ) | |
( set! currentFile ( car filelistSource ) ) | |
; Get open and get Image ID of current file | |
( set! theImage | |
( car ( gimp-file-load RUN-NONINTERACTIVE | |
currentFile currentFile ) ) ) | |
( if isLinux | |
; Target path-filename if Host OS is Linux | |
( begin | |
( set! baseName ( car ( reverse | |
( strbreakup currentFile "/" ) ) ) ) | |
( set! baseName (drop-ext baseName )) | |
( set! outFilename ( string-append | |
targetDirectory "/" baseName ".jpg" ) ) | |
) ; End begin - Host OS is Linux | |
; Target path-filename if Host OS is Windows | |
( begin | |
( set! baseName ( car ( reverse | |
( strbreakup currentFile "\\" ) ) ) ) | |
( set! baseName (drop-ext baseName) ) | |
( set! outFilename ( string-append | |
targetDirectory "\\" baseName ".jpg" ) ) | |
) ; End begin - if Host OS is Windows | |
) ; End if isLinux | |
; Check to see if outFilename exists so we don't overwrite | |
( while ( not ( null? checkFileExists ) ) | |
( set! checkFile ( car checkFileExists ) ) | |
( if ( string=? outFilename checkFile ) | |
( set! doIt #f ) ) | |
(set! checkFileExists ( cdr checkFileExists ) ) | |
) ; End while checkFileExists | |
( if doIt | |
( begin | |
;; -------------------------------------- | |
;; add white bg layer | |
(set! theBgLayer (car (gimp-layer-new theImage (car (gimp-image-width theImage)) (car (gimp-image-height theImage)) 1 "bg" 100 0))) | |
(gimp-image-insert-layer theImage theBgLayer 0 1) | |
(gimp-edit-fill theBgLayer 1) | |
;; change mode of original layer to HARDLIGHT-MODE (18) | |
(set! theOrigLayer (vector-ref (cadr (gimp-image-get-layers theImage)) 0)) | |
(gimp-layer-set-mode theOrigLayer 18) | |
; Get / set Drawable ID, need it for file save. | |
( set! theDrawable ( car | |
( gimp-image-merge-visible-layers theImage 0 ) ) ) | |
; Save file | |
( gimp-file-save | |
RUN-NONINTERACTIVE theImage theDrawable | |
outFilename outFilename ) | |
;; -------------------------------------- | |
) ; End begin | |
) ; End if doIt | |
( gimp-image-delete theImage ) | |
; Update while loop iteration parameter | |
(set! filelistSource ( cdr filelistSource ) ) | |
) ; End while | |
); End outer begin | |
) ; End outer if | |
( set! returnVal #t ) | |
) ; End let* | |
) ; End define | |
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= | |
( script-fu-register "script-fu-whitenbackground" ; Function Name | |
"1 ) Whiten JPG background (Directory)" ; Menu Label | |
"This script is an interactive script to whiten background of all of the jpegs | |
in a source directory to a target | |
directory. The script is designed to be run WITHOUT ANY IMAGE | |
LOADED. Runs from GIMP shell in Linux and Windows." | |
"Yawo KPOTUFE" ; Author | |
"2016, Yawo KPOTUFE" ; Copyright | |
"November 2016" ; Creation Date | |
"" ; Valid Image Type - No Image required | |
; We actually don't want any images open when we run this | |
; script, so it must be available from the menu when an | |
; image is not loaded. This script will determine the IDs | |
; of the Image and Drawable itself rather than having them | |
; passed as parameters. | |
; Interactive widgets | |
SF-DIRNAME "JPG Originals (source) Directory" "" | |
SF-DIRNAME "JOG Whitened (target) Directory" "" | |
) ; End script-fu-register | |
( script-fu-menu-register | |
"script-fu-whitenbackground" "<Image>/WhitenBackground") | |
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment