Posts Tagged gimp
Shrinking by GIMP Lisp
Image alteration really annoys me some times when the tools that are on offer to do a cheap job show their price through the results. In other words there have been times that my efforts to shrink and enlarge images have resulted in a, rough, artifact chocked image of a quality annoying to the eye. Normally this is the result of some cheap Python script which has worked well enough in the past for generating eyecons but doesn’t really scale that well. Naturally there are better tools out there like the GIMP that I can alter images with but they come at a price: manual process = my time. This can be very time consuming when you have a whole directory full of images of my children that need to be processed from just one weekend. In walks the GIMP scritps to save the day.
GIMP scripts really really appeal to me for two reasons; firstly I love the GIMP as a tool (ignoring its low image bit rate), but more importantly the scripting language of choice is Lisp. Lisp is cool. Don’t ask me why I really don’t know, I just really like it and have ever since I first met it on a DEC Vax at university about fifteen years ago. I’d almost assigned it to the place where Prolog has gone to die when I realised that its at the back of my favourite text editor Emacs. However Emacs has most of its useful stuff already written so I’ve not really had the cause to play in Lisp till now.
So last year I dug into the GIMP and Lisp and came up with a couple of scripts that I like to keep in ‘.gimp-2.2/scripts’ in my home directory. They run over a list of images or a single and perform a nice image shrink. We don’t need to to a scale up as our camera takes images that are too large to be useful on a screen anyway. So here’s the script.
image-shrink.scm
;file: image-shrink.scm
;location: ~/.gimp-2.2/scripts
;version: 0.1
;date: 2007-06-05
;
;definition: single image shrink
;explanation: shrinks proportionally by width
;examples:
; rm bike.jpg && cp bike.34.jpg bike.jpg && gimp -i -b '(image-shrink "bike.jpg" 500)' '(gimp-quit 0)'
; gimp -i -b '(image-shrink "/home/michael/Desktop/bike.jpg" 500)' '(gimp-quit 0)'
;code:
(define
(image-shrink filename width)
(let* (
(image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
(drawable (car (gimp-image-get-active-layer image)))
(iheight (car (gimp-image-height image)))
(iwidth (car (gimp-image-width image)))
)
(gimp-drawable-transform-scale
drawable
0.0 0.0
width
; iheight
(* iheight (/ width iwidth))
0 2
1 3 0
)
(gimp-file-save RUN-NONINTERACTIVE image drawable filename filename)
(gimp-image-delete image)
)
)
;
;definition: multi-image shrink
;explanation: calls image-shrink repeatedly to shrink files from a globbed list
;examples:
; rm bike.jpg && cp images/bike.full.34.jpg bike.jpg && gimp -i -b '(images-shrink "/home/michael/Desktop/bike.jpg" 500)' '(gimp-quit 0)'
; gimp -i -b '(images-shrink "/home/michael/Desktop/bike.jpg" 500)' '(gimp-quit 0)'
;code:
(define
(images-shrink pathname width)
(let* (
(filelist (cadr (file-glob path 1)))
)
(while
(not (null? filelist))
(let* (
(filename (car filelist))
)
(image-shrink filename width)
)
(set! filelist (cdr filelist))
)
)
)