Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Script-fu resize images in directory
#1
Hello all!  First post here so I hope this make sense.

I am trying to resize images to the highest multiple of 4 (unless it already is a multiple of 4).

My script runs without any errors but does absolutely nothing to the images in the directory.
I tested out individual elements of the script in the GIMP console (or whatever it is called) and the ones I could test work fine.

I feel like I'm missing something simple.

Any ideas?  The script is below. 

Code:
; example pattern form because it starts in C: "/Users/Etc/*.png"

(script-fu-register
           "script-fu-resize-4"                        ;func name
           "Resize To Multiple Of 4"                                  ;menu label
           "Rounds Canvas Size To Nearest Multiple Of 4" ;description
           "Me"                             ;author
           "copyright 2018, Me"                ;copyright notice    
           "May 30, 2018"                          ;date created
           ""                     ;image type that the script works on
           SF-STRING      "Folder Pattern"          "/*.png"  
           
 )
(script-fu-menu-register "script-fu-resize-4" "<Image>/File")
(define (script-fu-resize-4 pattern)
    (let*
        (
            (filelist (cadr (file-glob pattern 1)))
        )
        (while (not (null? filelist))
            (let*
                (    ; variables
                    (filename (car filelist))
                    (image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)) )
                    (drawable (car (gimp-image-get-active-layer image)) )
                    (width (car (gimp-image-width image)) )
                    (height (car (gimp-image-height image)) )
                )
                
                ; statements
                
                (set! width  (* 4 (ceiling (/ width  4))) )
                (set! height (* 4 (ceiling (/ height 4))) )
                (set! width  (inexact->exact width)  )
                (set! height (inexact->exact height) )
                (gimp-image-resize image width height 0 0)
                (gimp-file-save RUN-NONINTERACTIVE image drawable filename filename)
                (gimp-image-delete image)
            )
            
            ;move to next file in folder
            (set! filelist (cdr filelist))
        )
    )
)
Reply
#2
"Does nothing": files are not updated? Or files are changed but the image has the same size?

You can also use gimp-message to display the values of width/height just before resizing.
Reply
#3
(08-01-2018, 12:53 PM)Ofnuts Wrote: "Does nothing": files are not updated? Or files are changed but the image has the same size?

You can also use gimp-message to display the values of width/height just before resizing.

Thanks for the reply. The image is not being resized. 

I ran the script and it turns out it is actually changing the file (I'm just testing on one for now, in a folder).  I ran it today and the file says date modified of August 1st, however the dimensions of the image did not change at all.

I added some gimp-message statements and the intended new width and height are proper multiples of 4, so it seems the issue is with these lines:

Code:
(gimp-image-resize image width height 0 0)
(gimp-file-save RUN-NONINTERACTIVE image drawable filename filename)
(gimp-image-delete image)
It's like the resize isn't going through, and the file still gets updated.
Reply
#4
If I understand this correctly, you're resizing the image BUT leaving the layer (aka drawable) unchanged.

So when you come to save the result, gimp-file-save is saving the drawable - which is unchanged.

You might want to do gimp-layer-resize-to-image-size before saving.
Reply
#5
Got it... gimp-image-resize only resizes the canvas and not the layer(s) (like Image>Canvas size).  gimp-file-save saves the layer, which hasn't been resized... So you would have to also use gimp-layer-resize-to-image-size. Another method is to resize the layer directly (and not the image).
Reply
#6
I added, after resizing the image
Code:
(gimp-layer-resize-to-image-size drawable)

and it works now!

Thanks very much to you both!
Reply


Forum Jump: