You are here

Batch crop images... but keep centered

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
Rate this item!
AttachmentSize
batch-crop-center.scm1.3 KB

This is a small script for batch crop images and keep centered.

This is the script:
============

; 2012 Ivan Lantes Perez
; Script to crop images and keep centered

(define (batch-crop-center pattern
width
height)
(let* ((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))))
(let* ((original-width (car (gimp-image-width image)))
(original-height (car (gimp-image-height image)))
)
(if (<= (/ original-width original-height) (/ width height))
(gimp-image-crop image original-width (* original-width (/ height width)) 0 (/ (- original-height (* original-width (/ height width))) 2) )
(gimp-image-crop image (* original-height (/ width height)) original-height (/ (- original-width (* original-height (/ width height))) 2) 0)
)
)

(set! drawable (car (gimp-image-get-active-layer image)))
(gimp-file-save RUN-NONINTERACTIVE image drawable filename filename)
(gimp-image-delete image))

(set! filelist (cdr filelist))
)
)
)

Simple usage (under Windows):
=======================
"C:\Program files\Gimp-2.0\bin\gimp-2.6.exe" -i --verbose -b "(batch-crop-center \"C:\\images\\*.jpg\" 800 600)" -b "(gimp-quit 0)"

This is a sample, so if yout gimp-2.6.exe executable file is stored in other folder or partition of your hard disk change the path.
It's the same for the path of the images.
In this sample the final image width is 800 and the height is 600

Hope this script is useful for you guys.

Code License: 
GIMP Version: 
Scripting Engine: 
Subscribe to Comments for "Batch crop images... but keep centered"