0

我昨天刚开始为我需要的脚本编写 GIMP script-fu 脚本,但我不知道如何自动在局部变量中添加图层并调用将图层添加到图像的函数。我尝试遍历列表但优雅地失败了。有人可以将我推向正确的方向吗?

(define (script-fu-gifslot inText inFont inFontSize inTextColor)
    (let*
        ((theImageWidth  700) ; define our local variables
         (theImageHeight 100) ; create a new image:
         (theImage)
         (layerX)
         (inOffset)
         (theImage (car (gimp-image-new theImageWidth theImageHeight RGB)))
         (theText)             ;a declaration for the text

         (theLayer (car (gimp-layer-new theImage theImageWidth theImageHeight RGB-IMAGE "layer 1" 100 NORMAL)))
         (theLayer2 (car (gimp-layer-new theImage theImageWidth theImageHeight RGB-IMAGE "layer 2" 100 NORMAL)))
         (theLayer3 (car (gimp-layer-new theImage theImageWidth theImageHeight RGB-IMAGE "layer 3" 100 NORMAL)))
         (theLayer4 (car (gimp-layer-new theImage theImageWidth theImageHeight RGB-IMAGE "layer 4" 100 NORMAL))))

      (define (yannis-add-new-layer layerX inOffset)
          (gimp-image-add-layer theImage layerX 0)
        (gimp-context-set-background '(255 255 255))
        (gimp-context-set-foreground inTextColor)
        (gimp-drawable-fill layerX BACKGROUND-FILL)

        (set! theText (car (gimp-text-fontname theImage layerX 0 0 inText 0 TRUE inFontSize PIXELS "Sans")))
        (set! theImageHeight  (car (gimp-drawable-height theText)))

        (gimp-layer-resize layerX theImageWidth theImageHeight 0 0)
        (gimp-layer-set-offsets theText 0 (- 0 inOffset)))
      (yannis-add-new-layer theLayer 0)
      (yannis-add-new-layer theLayer2 10)
      (yannis-add-new-layer theLayer3 20)
      (yannis-add-new-layer theLayer4 30)
      (gimp-display-new theImage)
      (list theImage layerX theText)))

(script-fu-register 
 "script-fu-gifslot"
 "_GGGIF WORD Slotmachine..."
 "Creates an image with a user specified text string."
 "Yannis De Cleene <yannisdcl@gmail.com>"
 "Yannis De Cleene"
 "July, 2014"
 ""
 SF-TEXT       "Text"                "PASTE\nMULTILINE TEXT\nIN HERE"
 SF-FONT       "Font"                "Sans"
 SF-ADJUSTMENT "Font size (pixels)"  '(100 2 1000 1 10 0 1)
 SF-COLOR      "Color"               '(0 0 0))

(script-fu-menu-register "script-fu-gifslot" "<Image>/File/Create")
4

1 回答 1

3

使用mapfor-eachdo

如果要将 1 添加到列表中,可以使用(map 1+ '(1 2 3)). 或者,如果您想动态定义函数,请使用 lambda:(map (lambda (x) (+ 1 x)) '(1 2 3))

如果您想要更命令式的样式,请使用 do。你可以在这里阅读。

您可以使用函数搜索函数(和其他绑定符号)apropos。ej (apropos "for")(显然我并不在意)

但是我觉得你的问题不仅仅是遍历一个列表。例如,在 let 上定义函数是否有意义?为什么不明确地将图像传递给 add-new-layer-function?

为了使您的代码更具可读性,我会删除“the”,使用“-”而不是 camelCase,并且不要在每一行中留下一个双引号。它损害了可读性。我可以看到这不是你的错,因为 gimp 的教程中使用了这种风格。ej theImage 变为 image 并且 TheLayer1 变为 layer-1。

如果您想了解更多关于方案的信息,您应该下载球拍并阅读如何设计程序,这是一个很好的编程介绍。

于 2014-07-18T20:12:29.603 回答