Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Learning to write a script-fu 1

Status
Not open for further replies.

ibirman

Programmer
Aug 3, 2005
3
US
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:

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
 
I'm not sure how to do it in script-fu, but there is a menu option to resize the image to the size of the drawable, so I'd expect that you ought to be able to do the same thing via script-fu.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Thanks. My script is actually using the menu option, but doing it multiple times in small steps to get a sharper image. The line of code that does the scaling is "gimp-drawable-transform-scale", and it is working. What wasn't working before was the image crop, but I figured out that the reason it wasn't working was that my math was wrong. I have since figured out what I was doing wrong. Here is my final, working script:

Code:
; Resize Stairstep

; Author:    Igor Birman
; Date:      08/03/2005
; Version:   1.0

; This script will resize an image in several steps to give the sharpest 
; possible resized image.  An image can be scaled up or down using this 
; technique.  8-12 steps is considered to be the optimum amount.

; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version.
; 
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
; 
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

(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 the entire image
  (gimp-selection-all img)

  ; Retrieve the current dimensions of the image
  (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)

  ; Start a let group  
  (let* 
    (
      ; Calculate current and new width, height, ratio, and step size
      (width  (- x2 x1))
      (height (- y2 y1))
      (ratio  (/ height width))
      (step   (/ (- width new-width) number-of-steps))
      (w      width)
      (h      0)
    )

    ; Loop until the image is the desired size
    (while (not (= w new-width))
      (set! w (- w step))

      ; Allow for rounding, do not make it smaller than needed.
      ; If decreasing the size of the image..
      (if (< new-width width)
        (if (< w new-width)
          (set! w new-width)
        )
      )
      ; If increasing the size of the image..
      (if (> new-width width)
        (if (> w new-width)
          (set! w new-width)
        )
      )
      ; Set new height based on width times ratio 
      (set! h (* w ratio))

      ; Increase the canvas size to the new size if increasing image 
      (if (> new-width width)
        (gimp-image-resize img w h 0 0) 
      )

      ; 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
      (if (< new-width width)
        (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"
		    "Igor Birman"
		    "August 3, 2005"
		    ""
		    SF-IMAGE    "Image"         0
		    SF-DRAWABLE "Drawable"      0
		    SF-VALUE    "Target Width"  "1024"
		    SF-ADJUSTMENT  "Number of Steps"  '(3 2 12 1 1 1 1)
		    )
[/code]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top