3

I'm trying to draw a line using gimp scripting. In the following script: I create a new image 512x512 and a I try to draw a diagonal (0,0)->(512,512). But the image (tmp.xcf) remains transparent.

What am i doing wrong ?

(define (drawdiagonal W H)
(let* (
      (img 0)
       (bg 0)
       (points (cons-array 4 'double) )
      )
(set! img (car (gimp-image-new  W H 0)))
(gimp-image-undo-group-start img)
(gimp-context-push)
(set! bg (car (gimp-layer-new img  W H RGBA-IMAGE "background" 100 NORMAL-MODE)))
(gimp-image-add-layer img bg 0)
(gimp-drawable-set-visible bg TRUE)
(gimp-image-set-active-layer img bg)
(gimp-context-set-brush-size 10.0)
(gimp-context-set-opacity 100)
(gimp-context-set-paint-mode NORMAL-MODE)
(gimp-context-set-foreground '(255 127 0))
(gimp-selection-all img)

(aset points 0 0)
(aset points 1 0)
(aset points 2 W)
(aset points 3 H)
(gimp-paintbrush-default  bg 4 points)

(gimp-context-pop)
(gimp-image-undo-group-end img)

(gimp-xcf-save 1 img img "tmp.xcf"  "tmp.xcf")
(display "DONE")
)
)

(drawdiagonal 512 512) (gimp-quit 0)

usage:

cat test.scm | gimp -i  -b -
Pierre
  • 34,472
  • 31
  • 113
  • 192

2 Answers2

3

It seems you are mostly missing:

(gimp-context-set-brush "Some brush")

Additionally, the 3rd argument there (2nd img) should be a drawable:

(gimp-xcf-save 1 img img "tmp.xcf"  "tmp.xcf")

PS: do yourself a favor and write your script in Python. See here for an example.

Edit post-Gimp 2.10: there is now an API to stroke paths in "Line" mode which is the best way to obtain clean results. The relevant calls are:

pdb.gimp_context_set_stroke_method(STROKE_LINE)
pdb.gimp_context_set_line_cap_style(...)
pdb.gimp_context_set_line_join_style(...)
pdb.gimp_context_set_line_miter_limit(...)
pdb.gimp_context_set_line_width(width)
pdb.gimp_drawable_edit_stroke_item(drawable, path)
xenoid
  • 8,396
  • 3
  • 23
  • 49
  • 1
    very useful thanks ! (btw using gimp is a pretext to learn functional programming/scheme) – Pierre Mar 02 '18 at 00:14
  • 3
    Some of my Python scripts contain a lot more functional programming than the sum of all the Scheme Gimp scripts I have seen. Scriptfu's are mostly procedural things written with a functional programming language. – xenoid Mar 02 '18 at 09:02
1

For those who may come along later, based on xenoid's hints, I made this using gimp-pencil.

[I found no documentation for the 'item/path' of gimp_drawable_edit_stroke_item]

Could be tweaked to use gimp-brush...

(define (util-draw-path drawable args . path)
  ;; Draw a zig-zag line:
  ;; (util-draw-path layer `((width 10) (color ,RED)) 10 50 40 80 70 30 110 90 150 80)
  (let ((save  (util-assq 'save args) #t)
        (color (util-assq 'color args))   ; else use context value
        (width (util-assq 'width args))   ; else use context value
        (miter (util-assq 'miter args))   ; else use context value
        (join  (util-assq 'join args))    ; else use context value
        (cap   (util-assq 'cap args))     ; else use context value
        (stroke (util-assq 'stroke args)) ; else use context value
        )
    (and save (gimp-context-push))
    (and miter (gimp-context-set-line-miter-limit miter)) ; default: 10, default mitre up to 60 pixels
    (and stroke (gimp-context-set-stroke-method stroke))  ; default STROKE-PAINT-METHOD
    (and cap (gimp-context-set-line-cap-style cap))       ; CAP-ROUND, CAP-SQUARE
    (and join (gimp-context-set-line-join-style join))    ; JOIN-MITER, JOIN-ROUND, JOIN-BEVEL
    (and color (gimp-context-set-foreground color))       ;
    (and width (gimp-context-set-line-width width))       ; default: 6

    (let ((vec (apply vector path)))
      (gimp-pencil drawable (vector-length vec) vec))
    (and save (gimp-context-pop))
    ))

;;; (util-assq key list [default]) -> value or [default or #f]
(macro (util-assq form)
  (let ((key (cadr form)) (lis (caddr form))
        (els (if (pair? (cdddr form)) (cadddr form) #f)))
    `(let ((v (assq ,key ,lis)))
       (if v (cadr v) ,els))))
Jack Punt
  • 342
  • 1
  • 14