Resize script for gimp

Post Reply
Peter_w
Posts: 28
Joined: Tue Aug 19, 2008 9:28 am

Resize script for gimp

Post by Peter_w »

Hello,

I need to resize several jpeg-files.
I know you can do scripts with gimp,
so you can change several files at once.
But I'm not sure how.

I have ubuntu and gimp 2.6
chris
Site Admin
Posts: 202
Joined: Mon Jul 21, 2008 9:52 am

Re: Resize script for gimp

Post by chris »

I created a script that resizes all the images in a directory,
based on with and keeps the aspect ratio:

Code: Select all

(define (gimp_test_resize pattern img-width)
  (let*
    ;get list of files
    ((filelist (cadr (file-glob pattern 1))))
    (while (not (null? filelist))
           (let*
		  ((filename (car filelist))
                  (image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
		  (drawable (car (gimp-image-get-active-layer image)))

		  ;Calculate the new_height compared to the img-width
		  (cur-width  (car (gimp-image-width image)))
		  (cur-height (car (gimp-image-height image)))
		  (ratio (/ img-width cur-width))
		  (new-height (* ratio cur-height))
		  )

		  ;disable undo, makes the script faster
		  (gimp-image-undo-disable image)

		  ;the actual resize
		  (gimp-image-scale-full image img-width new-height INTERPOLATION-LANCZOS)
		  
		  ;Saving
		  (gimp-file-save RUN-NONINTERACTIVE image drawable new_filename new_filename)
		  (gimp-image-delete image)
	   )
           (set! filelist (cdr filelist))
    )
  )
)
Save it in /home/(user)/.gimp-2.6/scripts/gimp_test_resize.scm

Just go to the directory where you want to resize the pictures, and type following command:

Code: Select all

gimp -i -b '(gimp_test_resize "*.jpg" 150)' -b '(gimp-quit 0)'
150 is the new width, can be any number.

Just one bad thing about the script,
it overwrites the images it resizes, so it is a good idea to copy everything first.

I'm working to make a better version that allows a prefix or to save to another directory.
chris
Site Admin
Posts: 202
Joined: Mon Jul 21, 2008 9:52 am

Re: Resize script for gimp

Post by chris »

I updated the script:

Here is the new version:

Code: Select all

(define (gimp_test_resize pattern img-width dir prefix)
  (let*
    ;get list of files
    ((filelist (cadr (file-glob pattern 1))))
    (while (not (null? filelist))
           (let*
		  ((filename (car filelist))
                  (image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
		  (drawable (car (gimp-image-get-active-layer image)))

		  ;Calculate the new_height compared to the img-width
		  (cur-width  (car (gimp-image-width image)))
		  (cur-height (car (gimp-image-height image)))
		  (ratio (/ img-width cur-width))
		  (new-height (* ratio cur-height))

		  ;Creating the new filename
		  (new_filename (string-append dir prefix filename))
		  )

		  ;disable undo, makes the script faster
		  (gimp-image-undo-disable image)

		  ;the actual resize
		  (gimp-image-scale-full image img-width new-height INTERPOLATION-LANCZOS)
		  
		  ;Saving
		  (gimp-file-save RUN-NONINTERACTIVE image drawable new_filename new_filename)
		  (gimp-image-delete image)
	   )
           (set! filelist (cdr filelist))
    )
  )
)
You can now save it in another directory or add a prefix:
Ideal for thumbnails:

Code: Select all

gimp -i -b '(gimp_test_resize "*.jpg" 150 "" "thumb_")' -b '(gimp-quit 0)'
or in another directory

Code: Select all

gimp -i -b '(gimp_test_resize "*.jpg" 150 "../thumbs/" "small_pics_")' -b '(gimp-quit 0)'

I had some help from the following sites:
http://www.gimp.org/tutorials/Basic_Batch/
http://episteme.arstechnica.com/eve/for ... 4002906931
http://www.gimptalk.com/forum/script-fu ... t9440.html
http://www.home.unix-ag.org/simon/gimp/export-file.scm
chris
Site Admin
Posts: 202
Joined: Mon Jul 21, 2008 9:52 am

Re: Resize script for gimp

Post by chris »

Some people had problems with GIMP 2.6.
So here is the updated version:

Code: Select all

(define (gimp_test_resize pattern img-width dir prefix)
  (let* ((filelist (cadr (file-glob pattern 1)))) ;Get list of files
    (while (not (null? filelist))
      (let* ((filename (car filelist))
	(new_filename (string-append dir prefix filename))
	(image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
	(drawable (car (gimp-image-get-active-layer image)))
      )

      (let*
      (
        ;Calculate the new_height compared to the img-width
        (cur-width  (car (gimp-image-width image)))
        (cur-height (car (gimp-image-height image)))
        (ratio (/ img-width cur-width))
        (new-height (* ratio cur-height))
      )

        ;disable undo, makes the script faster
        (gimp-image-undo-disable image)

        ;the actual resize
        (gimp-image-scale image img-width new-height) ;change for gimp2.6
	)

        ;Saving
        (gimp-file-save RUN-NONINTERACTIVE image drawable new_filename new_filename)
        (gimp-image-delete image)
      )
      (set! filelist (cdr filelist))
    )
  )
)
Many thanks to Wim
Post Reply