Friday, November 14, 2008

GIMP Repeating Guidelines

Update (18 Nov): bug fix - guides go up to both edges, if width/height is a multiple of the separation distance. max was set to height (if vertical guide) when it should have been width and vice versa for vertical.

The GIMP photo/image manipulation program allows you to add guides as an aid to image editing. It is fairly simple to add them - Image > Guides > New Guide or click on one of the rulers and drag onto the canvas. However, this can be time consuming if you need to add multiple guides, equally spaced.

Script-Fu however, allows you to do this via scripting. First of all, create a scm file (e.g. guides-repeat.scm) in your script folder (on Windows this would be C:\Program Files\GIMP-2.0\share\gimp\2.0\scripts (all users) or %USERPROFILE%\.gimp-2.6\scripts, then paste in the following code (use a plain text editor like Notepad)

(define (script-fu-guide-repeat image
                             drawable
                             direction
                             separation)
  (let* (
        (width (car (gimp-image-width image)))
        (height (car (gimp-image-height image)))
  (position 0)
  )
 ; set maximum otherwise the guides will be added indefinitely
 (if (= direction 0)
   (set! max height)
   (set! max width)
 )
 ; while the position is less than the maximum
 (while (<= position max)

  (if (= direction 0)
   ;; check position is inside the image boundaries
   (if (<= position height) (gimp-image-add-hguide image position))
   (if (<= position width) (gimp-image-add-vguide image position))
  )
  
 ; increment position by defined separation 
 (set! position (+ position separation)))
 (gimp-displays-flush)
  )
)

(script-fu-register "script-fu-guide-repeat"
  _"R_epeating Guide"
  _"Add multiple guides separated by specified number of pixels"
  "Sam Collett"
  "Sam Collett"
  "2008-11-18"
  "*"
  SF-IMAGE      "Image"      0
  SF-DRAWABLE   "Drawable"   0
  SF-OPTION     _"Direction" '(_"Horizontal" _"Vertical")
  SF-ADJUSTMENT _"Separation"  (list 40 1 MAX-IMAGE-SIZE 1 10 0 1)
)

(script-fu-menu-register "script-fu-guide-repeat"
                         "<Image>/Image/Guides")

After saving the script, either restart GIMP or Filters > Script-Fu > Refresh Scripts (GIMP 2.6, earlier versions use Xtns instead of Filters). Repeating Guides should now appear under Image > Guides

No comments: