Skip to content

Instantly share code, notes, and snippets.

@yawo
Last active November 10, 2016 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yawo/eaa8447a2d4f944b17cdf83580517aa2 to your computer and use it in GitHub Desktop.
Save yawo/eaa8447a2d4f944b17cdf83580517aa2 to your computer and use it in GitHub Desktop.
this square you img to 1000px on a white background
; extract basename without ext
(define (drop-ext str)
(unbreakupstr (butlast (strbreakup str ".")) ".")
)
(define (maxsize img)
(max (car (gimp-image-width img)) (car (gimp-image-height img)))
)
(define (offsets img)
(let*
(
(width (car (gimp-image-width img)))
(height (car (gimp-image-height img)))
(maxSize (max width height))
(ofX (/ (- maxSize width) 2))
(ofY (/ (- maxSize height) 2))
)
(list ofX ofY)
)
)
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
( define ( script-fu-square sourceDirectory )
( 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]" ) ) )
; List of files to be converted formatted for current Host
; O/S
( filelistSource ( cadr ( file-glob patternSource 1 ) ) )
; Place holders for image variables - updated per image
( theImage 0 )
( theOrigLayer 0 )
( theBgLayer 0 )
( theSize 0 )
( theOfs nil )
( theDrawable 0 )
( currentFile "" )
( baseName "" )
) ; 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! currentFile ( car filelistSource ) )
; Get open and get Image ID of current file
( set! theImage
( car ( gimp-file-load RUN-NONINTERACTIVE
currentFile currentFile ) ) )
( begin
;; --------------------------------------
;; define the size
(set! theSize (maxsize theImage))
(set! theOfs (offsets theImage))
;; add white bg layer
(set! theBgLayer (car (gimp-layer-new theImage theSize theSize 1 "bg" 100 0)))
(gimp-image-insert-layer theImage theBgLayer 0 1)
(gimp-edit-fill theBgLayer 1)
(set! theOrigLayer (vector-ref (cadr (gimp-image-get-layers theImage)) 0))
(gimp-image-resize-to-layers theImage)
(gimp-layer-translate theOrigLayer (car theOfs) (cadr theOfs))
(gimp-image-scale theImage 1000 1000)
; 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
currentFile currentFile )
;; --------------------------------------
) ; End begin
( 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-square" ; Function Name
"1 ) Square JPG (Directory)" ; Menu Label
"This script is an interactive script to square all of the jpegs
in a source 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" ""
) ; End script-fu-register
( script-fu-menu-register
"script-fu-square" "<Image>/Square")
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment