1

I found this wonderful script on github and used it successfully with GIMP on Windows 7. I recently upgraded to Windows 10, and now it will not work. I get the following error:

Error while executing script-fu-batch-smart-resizer:

Error: ( : 1) eval: unbound variable: strbreakup

Here is the code:

; https://github.com/per1234/batch-smart-resize
(define (script-fu-batch-smart-resize sourcePath destinationPath filenameModifier outputType outputQuality maxWidth maxHeight pad padColor . JPEGDCT)
  (define (smart-resize fileCount sourceFiles)
    (let*
      (
        (filename (car sourceFiles))
        (image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
      )
      (gimp-image-undo-disable image)

      ;crop to mask if one exists
      (if (not (= (car (gimp-layer-get-mask (car (gimp-image-get-active-layer image)))) -1)) (plug-in-autocrop RUN-NONINTERACTIVE image (car (gimp-layer-get-mask (car (gimp-image-get-active-layer image))))))

      ;image manipulation
      (let*
        (
          ;get cropped source image dimensions
          (sourceWidth (car (gimp-image-width image)))
          (sourceHeight (car (gimp-image-height image)))

          ;don't resize image to larger than original dimensions
          (outputMaxWidth (if (< sourceWidth maxWidth) sourceWidth maxWidth))
          (outputMaxHeight (if (< sourceHeight maxHeight) sourceHeight maxHeight))

          (outputWidth (if (< (/ sourceWidth sourceHeight) (/ outputMaxWidth outputMaxHeight)) (* (/ outputMaxHeight sourceHeight) sourceWidth) outputMaxWidth))
          (outputHeight (if (> (/ sourceWidth sourceHeight) (/ outputMaxWidth outputMaxHeight)) (* (/ outputMaxWidth sourceWidth) sourceHeight) outputMaxHeight))
        )
        (gimp-image-scale image outputWidth outputHeight)  ;scale image to the output dimensions

        ;pad
        (if (= pad TRUE)
          (begin
            (gimp-image-resize image maxWidth maxHeight (/ (- maxWidth outputWidth) 2) (/ (- maxHeight outputHeight) 2))  ;resize canvas to to maximum dimensions and center the image

            ;add background layer
            (let*
              (
                (backgroundLayer (car (gimp-layer-new image maxWidth maxHeight RGB-IMAGE "Background Layer" 100 NORMAL-MODE)))  ;create background layer
              )
              (let*
                (
                  (backgroundColor (car (gimp-context-get-background)))  ;save the current background color so it can be reset after the padding is finished
                )
                (gimp-context-set-background padColor)  ;set background color to the padColor
                (gimp-drawable-fill backgroundLayer 1)  ;Fill the background layer with the background color. I have to use 1 instead of FILL-BACKGROUND because GIMP 2.8 uses BACKGROUND-FILL.
                (gimp-context-set-background backgroundColor)  ;reset the background color to the previous value
              )
              (gimp-image-insert-layer image backgroundLayer 0 1)  ;add background layer to image
            )
          )
        )
      )

      (gimp-image-flatten image)  ;flatten the layers


      (let*
        (
          ;format filename - strip source extension(from http://stackoverflow.com/questions/1386293/how-to-parse-out-base-file-name-using-script-fu), add filename modifier and destination path
          (outputFilenameNoExtension
            (string-append
              (string-append destinationPath "/")
              (unbreakupstr
                (reverse
                  (cdr
                    (reverse
                      (strbreakup
                        (car
                          (reverse
                            (strbreakup filename (if isLinux "/" "\\"))
                          )
                        )
                        "."
                      )
                    )
                  )
                )
                "."
              )
              filenameModifier
            )
          )
        )

        ;save file
        (cond
          ((= outputType 0)
            (let*
              (
                (outputFilename (string-append outputFilenameNoExtension ".png"))  ;add the new extension
              )
              (file-png-save RUN-NONINTERACTIVE image (car (gimp-image-get-active-drawable image)) outputFilename outputFilename FALSE 9 TRUE FALSE FALSE TRUE TRUE)
            )
          )
          ((= outputType 1)

            (let*
              (
                (outputFilename (string-append outputFilenameNoExtension ".jpg"))  ;add the new extension
              )
              (file-jpeg-save RUN-NONINTERACTIVE image (car (gimp-image-get-active-drawable image)) outputFilename outputFilename (/ outputQuality 100) 0 TRUE TRUE "" 2 TRUE 0 (if (null? JPEGDCT) 0 (car JPEGDCT)))
            )
          )
          (else
            (let*
              (
                (outputFilename (string-append outputFilenameNoExtension ".gif"))  ;add the new extension
              )
              (gimp-image-convert-indexed image 1 0 256 TRUE TRUE "")
              (file-gif-save RUN-NONINTERACTIVE image (car (gimp-image-get-active-drawable image)) outputFilename outputFilename FALSE FALSE 0 0)
            )
          )
        )
      )
      (gimp-image-delete image)
    )
    (if (= fileCount 1) 1 (smart-resize (- fileCount 1) (cdr sourceFiles)))  ;determine whether to continue the loop
  )

  ;detect OS type(from http://www.gimp.org/tutorials/AutomatedJpgToXcf/)
  (define isLinux
    (>
      (length (strbreakup sourcePath "/" ) )  ;returns the number of pieces the string is broken into
      (length (strbreakup sourcePath "\\" ) )
    )
  )
  (define sourceFilesGlob (file-glob (if isLinux (string-append sourcePath "/*.*") (string-append sourcePath "\\*.*")) 0))
  (if (pair? (car (cdr sourceFilesGlob)))  ;check for valid source folder(if this script is called from another script they may have passed an invalid path and it's much more helpful to return a meaningful error message)
    (smart-resize (car sourceFilesGlob) (car (cdr sourceFilesGlob)))
    (error (string-append "Invalid Source Folder " sourcePath))
  )
)
;dialog
(script-fu-register
  "script-fu-batch-smart-resize"  ;function name
  "batch-smart-resize"  ;menu label
  "Crop to layer mask, resize within maximum dimensions, and pad to max dimensions(optional)"  ;description
  "per1234"  ;author
  ""  ;copyright notice
  "2015-10-02"  ;date created
  ""  ;image type
  SF-DIRNAME "Source Folder" ""  ;sourcePath
  SF-DIRNAME "Destination Folder" ""  ;destinationPath
  SF-STRING "Output Filename Modifier(appended)" ""  ;filenameModifier
  SF-OPTION "Output Type" '("PNG" "JPEG" "GIF")  ;outputType
  SF-VALUE "Output Quality(JPEG only) 0-100" "90"  ;outputQuality
  SF-VALUE "Max Width" "1500"  ;maxWidth
  SF-VALUE "Max Height" "1500"  ;maxHeight
  SF-TOGGLE "Pad" TRUE  ;pad
  SF-COLOR "Padding Color" "white"  ;padColor
)
(script-fu-menu-register "script-fu-batch-smart-resize"
                         "<Image>/Tools")  ;menu location

I have tried just about everything I could find online, and this is my last resort. Am I missing syntax that was acceptable on the Windows 7 version, that is not so in the Windows 10 version?

Thanks!

Three-D
  • 75
  • 1
  • 8
  • @ghosh ..... you led me to the answer, with your question! I found this...."the string split function strbreakup is defined in the compatibility file from SIOD to TinyScheme: "C:\Program Files\GIMP\share\gimp\2.0\scripts\script-fu-compat.init". So, I copied the init files from the Program Files Dir, to the User Dir, where my scripts are saved. Works! – Three-D Apr 05 '18 at 20:21
  • 1
    Maybe you have dropped that directory. Is it listed in `Edit>Preferences>Folders>Scripts` (the WIn7 to WIn10 could have made a minute difference). – xenoid Apr 05 '18 at 20:24

1 Answers1

2

strbreakup is defined in script-fu-compat.init in /usr/share/gimp/2.0/scripts (or C:\Program Files\GIMP 2\share\gimp\2.0\scripts for Windows). Is this file present and complete (372 lines in my working version)?

Edit: summary from comments: Gimp didn't look in its standard scripts directory. The directory above should be listed in Edit>Preferences>Folders>Scripts.

xenoid
  • 8,396
  • 3
  • 23
  • 49
  • it is now in that directory. Thanks! – Three-D Apr 05 '18 at 20:23
  • 1
    Can you explain why it was missing? There is a [similar problem here](https://www.gimp-forum.net/Thread-FxCopyrightEgger-malfunction) – xenoid Apr 05 '18 at 20:30
  • Yes, when they did the install of my software, they did it using admin rights, which I am not granted (but was on my old machine). So when I tried to load my script files to the new location (C:\Program Files\GIMP 2\share\gimp\2.0\scripts), I was not able to there, but was able to in "C:\Users\MyUserID\.gimp-2.8\scripts" But, they did not work properly, giving the noted error in this post. So, by copying the .init files (that contained the definition of "strbreakup" from the "C:\Program Files\GIMP 2\share\gimp\2.0\scripts" to the "C:\Users\MyUserID\.gimp-2.8\scripts" fixed it. – Three-D Apr 05 '18 at 20:43
  • This means that `C:\Program Files\GIMP 2\share\gimp\2.0\scripts` isn't taken in account as it should. You may have other problems with other scripts. – xenoid Apr 05 '18 at 21:47
  • Thanks for pointing that out. I ended up adding both of the folders (user and program roots) to my script folders, under Edit>Preferences. Prior to that, there had only been the user folder. – Three-D Apr 06 '18 at 12:37
  • 2
    Ok, that explains it. – xenoid Apr 06 '18 at 13:12
  • @Three-D any idea who or what had removed the folder previously? It is present by default, but someone (maybe you) might have decided to remove it. – Michael Schumacher Apr 09 '18 at 07:11
  • @MichaelSchumacher I did.....they were default, and I thought I would "customize", lol. Now I know :) – Three-D Apr 09 '18 at 12:30