Hi,
I am trying to learn how to write a script for The Gimp. I have figured out the basics, but I am stuck on cropping the image.
The script I am writing is designed to resize an image to a smaller size in a series of steps. The result should be sharper than resizing all in one step. The script takes two parameters, a target size and the number of steps.
When I run my script, it will run through the required number of steps, but instead of cropping the entire image, my crop seems to be cropping the drawable, so I end up with a big image with a portion of my scaled drawable in the middle.
Can anyone tell me what I am doing wrong?
Here is the script:
Thanks,
Igor
I am trying to learn how to write a script for The Gimp. I have figured out the basics, but I am stuck on cropping the image.
The script I am writing is designed to resize an image to a smaller size in a series of steps. The result should be sharper than resizing all in one step. The script takes two parameters, a target size and the number of steps.
When I run my script, it will run through the required number of steps, but instead of cropping the entire image, my crop seems to be cropping the drawable, so I end up with a big image with a portion of my scaled drawable in the middle.
Can anyone tell me what I am doing wrong?
Here is the script:
Code:
;;; Author: Igor Birman
;;; Date: 08/03/2005
;;; Version: 0.1
(define (resize-stairstep img
drawable
new-width
number-of-steps
)
; Start an undo group. Everything between the start and the end will
; be carried out if an undo command is issued.
(gimp-image-undo-group-start img)
; Select all so we can calculate the image size
(gimp-selection-all img)
; Get the image size and store in x1, x2, y1, and y2
(set! selection-bounds (gimp-selection-bounds img))
(set! x1 (cadr selection-bounds))
(set! y1 (caddr selection-bounds))
(set! x2 (- (cadr (cddr selection-bounds)) x1))
(set! y2 (- (caddr (cddr selection-bounds)) y1))
; De-select the selection
(gimp-selection-none img)
; Calculate the image width, height, and ratio
(set! width (- x2 x1))
(set! height (- y2 y1))
(set! ratio (/ height width))
; Calculate the step size
(set! step (- width new-width))
(set! step (/ step number-of-steps))
; Loop through the required number of steps
(set! w width)<
(while (> w new-width)
(set! w (- w step))
(set! h (* w ratio))
(if (> w new-width)
; Scale the image to the new width and height
(gimp-drawable-transform-scale drawable 0 0 w h 0 2 0 3 0)
; Crop the image down to the new scaled size
(gimp-image-crop img w h 0 0)
; Re-display the cropped image
(gimp-displays-flush)
)
)
; Remove any existing selections
(gimp-selection-none img)
; Stop the undo group.
(gimp-image-undo-group-end img)
; Flush the display to show changes
(gimp-displays-flush)
)
(script-fu-register "resize-stairstep"
"<Image>/Script-Fu/Misc/Resize by Steps"
"Resize an image by steps"
"Igor Birman"
"Copyright Igor Birman 2005"
"August 3, 2005"
""
SF-IMAGE "Image" 0
SF-DRAWABLE "Drawable" 0
SF-VALUE "Target Width" "1024"
SF-VALUE "Number of Steps" "3"
)
Thanks,
Igor