【问题标题】:lisp iterate through listlisp 遍历列表
【发布时间】:2014-07-17 10:26:45
【问题描述】:

我昨天刚开始为我需要的脚本编写 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")

【问题讨论】:

  • 另一个选项,除非您已经熟悉 scheme/lisp,否则使用 Python 绑定来自动化 GIMP。语言对你的影响要小得多。

标签: scheme lisp gimp script-fu


【解决方案1】:

使用mapfor-eachdo

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

如果您想要更命令式的风格,请使用 do。你可以阅读它here

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

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

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

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

【讨论】:

    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    相关资源
    最近更新 更多