GIMP脚本语言: 画一条简单的线

3
我试图使用GIMP脚本绘制一条线。在以下脚本中,我创建了一个新的512x512图像,并尝试绘制一条对角线(0,0)->(512,512)。但是图像(tmp.xcf)仍然是透明的。
我做错了什么?
(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)

用法:

cat test.scm | gimp -i  -b -
2个回答

3

看起来你大多数缺失的是:

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

此外,那里的第三个参数(第二个 img)应该是可绘制对象:

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

提示:为自己做好事,用Python编写你的脚本。点击这里查看示例。

Gimp 2.10后编辑:现在有一个API可以使用“线”模式描绘路径,这是获得清晰结果的最佳方法。相关调用如下:

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)

1
非常有用,谢谢!(顺便说一句,使用GIMP是学习函数式编程/ Scheme的借口) - Pierre
3
我的一些Python脚本包含了比我看到的所有Scheme Gimp脚本的函数式编程更多的内容。Scriptfu主要是使用函数式编程语言编写的过程式代码。 - xenoid

1

对于那些可能会在之后遇到此问题的人,根据xenoid的提示,我使用gimp-pencil进行了制作。

[我没有找到有关gimp_drawable_edit_stroke_item中“item / path”的文档]

可以进行微调以使用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))))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接