script-fu + sh: jpeg resize scripts
GIMP supports scheme scripting via TinyScheme, the core of the GIMP script-fu interpreter. Great tool, very powerful. Used for making GIMP plugins and such.
"Learn scheme/script-fu" has been on my to-do list for a long time. I finished an intro this weekend.
Attached: a short scheme script to resize a single jpeg image to 800x600 (or 600x800, if it's portrait dimensions). Also, I wrote a little sh script to apply the resize to every jpeg in a directory.
Is this the best way to do bulk resizing? Highly likely not. But it works for me, using tools I have.
thomas-resize.scm
;; thomas-resize.scm
;; resizing script for jpeg images
;; written in script-fu (scheme) for GIMP
;; register the script with GIMP script-fu. This is a required function call.
;; registration actually occurs at (1) GIMP startup or (2) via Script-Fu -> Refresh Scripts
;; The script also needs to be placed in a menu - for this we use script-fu-menu-register (the next function call).
;; Again, these two calls are required. Fill in your necessary info.
( script-fu-register
"script-fu-make-800x600" ;;function name
"Make 800x600" ;; menu label
"Create 800x600 from existing jpeg" ;;description
"Denton Thomas" ;;author
"(c) 2010" ;;copyright
"Mar 2010" ;;creation date
"" ;;acceptable image types
SF-FILENAME "filename" "filename" ;;supports regular expressions
)
(script-fu-menu-register "script-fu-make-800x600" "/File/Create")
;; define the main function.
;; follow it with the script name and arguments, then the code
( define (script-fu-make-800x600 inFilename)
;; actual main code:
(let*
(;;in let:
;; variable declarations
(theWidth 800) ;; target width
(theHeight 600) ;; target height
(theDPI 72) ;; target resolution
(theImage 0)
(theImageHeight 0)
(theImageWidth 0)
(theBaseFilename "")
(theOutputFilename "")
(theDrawable 0)
);; end variable declarations
;; load our image
(set! theImage
(car (file-jpeg-load RUN-NONINTERACTIVE inFilename inFilename) )
) ;; end set! theImage
;; determine whether it's fat or wide
(set! theImageHeight
(car (gimp-image-height theImage) )
)
(set! theImageWidth
(car (gimp-image-width theImage) )
)
;;reduce DPI via set-resolution
(gimp-image-set-resolution theImage theDPI theDPI)
;;set dimensions
(if (< theImageHeight theImageWidth)
( gimp-image-scale theImage theWidth theHeight) ;; height < width
( gimp-image-scale theImage theHeight theWidth) ;; width < height
) ;; end if
;; now rename. We'll have to do a little trick to append _800x600 to
;; the file name. Note that this will fail horribly if there is a period
;; "." character in the filename before the extension!
(set! theBaseFilename ;; get the base filename
(car (strbreakup inFilename ".") )
)
(set! theOutputFilename ;; append the base to the new extension
(string-append theBaseFilename "_800x600.jpg")
)
;;flatten. Capture the drawable, as we need that for saving.
(set! theDrawable
(car (gimp-image-flatten theImage))
)
;; clear all the dirty bits before saving, just in case.
(gimp-image-clean-all theImage)
(file-jpeg-save RUN-NONINTERACTIVE ;; non-interactive
theImage
theDrawable ;; drawable to save
theOutputFilename ;; file name
theOutputFilename
.80 ;; save quality (0 to 1)
0 ;; smoothing
1 ;; optimize entropy encoding
1 ;; enable progressive image loading
"" ;; comment
0 ;; subsampling (?)
1 ;; force baseline creation (req'd for some decoders)
0 ;; restart markers
0 ;; dct algorithm (?)
)
);; end let
);; end define
resize-800x600.sh
#!/bin/sh
#resize-800x600.sh
# make a list of all the jpegs in the current directory
# resize them all to 800x600 (or 600x800) using gimp
# we'll call script-fu-make-800x600, a custom script-fu script
# sanity checking:
[ -z `which gimp` ] && echo "I couldn't find gimp! Need that." && exit 1
# our final string:
SCHEMESTRING=""
echo "`find *.jpg`" |
{
while read FILE # create the SCHEMESTRING
do
echo "FILE: "$FILE""
SCHEMESTRING="$SCHEMESTRING (script-fu-make-800x600 \""$FILE"\")"
done
# tack on the shutdown call:
SCHEMESTRING="$SCHEMESTRING (gimp-quit 1)"
#echo "Calling gimp with batch string: \""$SCHEMESTRING"\""
# call gimp!
gimp -i -b \'"$SCHEMESTRING"\'
}

Comments
Post new comment