【问题标题】:How do I write a custom auto-crop script using the GIMP?如何使用 GIMP 编写自定义自动裁剪脚本?
【发布时间】:2011-08-14 06:13:56
【问题描述】:

我有一堆屏幕截图,我想裁剪掉窗口边框。我想使用脚本将它们全部裁剪。

我可以访问 GIMP,但不能访问 Photoshop,所以我认为 GIMP 是最好的工具。我之前没有使用 GIMP 编写脚本,所以我查找了一些 GIMP 裁剪脚本。我找到的都与我想要的相似,但不完全一样。我认为将脚本更改为我需要的内容将是一件简单的事情。但是由于我不熟悉脚本语言,所以事实证明它比我想象的要困难。我发现了一个很棒的自动裁剪脚本here。有人可以帮我根据需要定制它吗?

    (define (script-fu-rs-center-crop filename outfilename width height)

  (let* ((image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
          (drawable (car (gimp-image-get-active-layer image))))
          (let* ((original-width (car (gimp-image-width image)))
               (original-height (car (gimp-image-height image)))
               (new-width original-width)
               (new-height original-height)
               (offset-x 0)
               (offset-y 0))

               (if (<= (/ original-width original-height) (/ width height))
                   (gimp-image-crop image original-width (* original-width (/ height width)) 0 (/ (- original-height (* original-width (/ height width))) 2) )
                   (gimp-image-crop image (* original-height (/ width height)) original-height (/ (- original-width (* original-height (/ width height))) 2) 0)
               )
           )

  (set! drawable (car (gimp-image-get-active-layer image)))

(gimp-file-save RUN-NONINTERACTIVE image drawable outfilename outfilename)
     (gimp-image-delete image)))

所有照片的整体尺寸并不相同。但它们在顶部、左侧、右侧和底部的像素数量都相同,我想裁剪出来。因此,假设图像的像素尺寸为widthheight,我想删除顶部的t_junk 像素、底部的b_junk 像素、左侧的l_junk 像素和r_junk 的像素正确的。所以我希望新的图像尺寸为width - l_junk - r_junkheight - t_junk - b_junk

【问题讨论】:

  • 为什么不使用imagemagickconvert 方法可以很简单。

标签: linux scripting image-processing crop gimp


【解决方案1】:

边缘脚本

我编写了一个自定义 GIMP 脚本,它完全符合您的要求。只需将以下内容粘贴到文本文档中,并在您的 GIMP 脚本文件夹中使用 .scm 扩展名保存它(路径可以在 Edit &gt; Preferences &gt; Folders &gt; Scripts 中找到/创建):

(define (script-fu-wirebear-edger filename outfilename top right bottom left)
(let* (
    (img (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
    (owidth (car (gimp-image-width img)))
    (oheight (car (gimp-image-height img)))
    (width (- owidth (+ right left)))
    (height (- oheight (+ top bottom)))
      )

   ;Crop Image
    (gimp-image-crop img width height left top)

   ;Save
    (gimp-file-save RUN-NONINTERACTIVE
        img
        (car (gimp-image-active-drawable img))
        outfilename
        outfilename)

   ;Cleanup
    (gimp-image-delete img)
))
(script-fu-register "script-fu-wirebear-edger"
    "Edger"
    "Removes junk from the edges of an image"
    "Chris Kent"
    "WireBear.com"
    "August 2011"
    "RGB* GRAY*"
    SF-STRING "Filename" ""
    SF-STRING "OutputFilename" ""
    SF-VALUE "TopEdge" "0"
    SF-VALUE "RightEdge" "0"
    SF-VALUE "BottomEdge" "0"
    SF-VALUE "LeftEdge" "0"
)
script-fu-wirebear-edger()

这个脚本接受输入文件名、输出文件名和每边要剃掉的像素数。您可以从 Windows 运行命令 (假设您将 GIMP 设置为环境变量) 像这样 (确保如图所示转义特殊字符并将所有字符放在一行上)

C:>gimp-2.6 -i -c -b
   "(script-fu-wirebear-edger \"C:\\Users\\You\\Desktop\\Images\\1.png\" 
   \"C:\\Users\\You\\Desktop\\Images\\1_edged.png\" 10 30 25 5)"
   -b "(gimp-quit 0)"

或者您可以在 Script-Fu 控制台 (Filters &gt; Script-Fu &gt; Console) 中运行它 - 无论像这样的操作系统:

(script-fu-wirebear-edger "C:\\Users\\You\\Desktop\\Images\\1.png" 
"C:\\Users\\You\\Desktop\\Images\\1_edged.png" 10 30 25 5)

批处理边缘脚本

为了在多个图像上运行 Edger 脚本,您可以将以下脚本与上述脚本结合使用(您的 Scripts 文件夹中都需要这两个脚本):

(define (script-fu-wirebear-batch-edger pattern outsuffix top right bottom left)
(let* (
    (filelist (cadr (file-glob pattern 1)))
    (filename "")
    (outfn "")
      )
    (while (not (null? filelist))
        (set! filename (car filelist))
        (set! outfn 
            (string-append 
                (string-append 
                    (substring filename 0 (- (string-length filename) 4))
                    outsuffix)
                (substring filename (- (string-length filename) 4))
            )
        )
        (script-fu-wirebear-edger filename outfn top right bottom left)
        (set! filelist (cdr filelist))
    )
))
(script-fu-register "script-fu-wirebear-batch-edger"
    "Batch Edger"
    "Removes junk from the edges of a series of images"
    "Chris Kent"
    "WireBear.com"
    "August 2011"
    "RGB* GRAY*"
    SF-STRING "Pattern" "*.png"
    SF-STRING "OutputSuffix" "_edged"
    SF-VALUE "TopEdge" "0"
    SF-VALUE "RightEdge" "0"
    SF-VALUE "BottomEdge" "0"
    SF-VALUE "LeftEdge" "0"
)
script-fu-wirebear-batch-edger()

脚本采用搜索模式来匹配目标图像、添加到文件名的后缀以及每张图像每侧要剃掉的像素数。您可以从 Windows 运行命令 (假设您将 GIMP 设置为环境变量) 像这样 (确保如图所示转义特殊字符并将所有字符放在一行上)

C:>gimp-2.6 -i -c -b
   "(script-fu-wirebear-batch-edger \"C:\\Users\\You\\Desktop\\Images\\*.png\" 
   \"_edged\" 10 30 25 5)"
   -b "(gimp-quit 0)"

或者您可以在 Script-Fu 控制台 (Filters &gt; Script-Fu &gt; Console) 中运行它——不管像这样的操作系统:

(script-fu-wirebear-batch-edger "C:\\Users\\You\\Desktop\\Images\\*.png"
"_edged" 10 30 25 5)

【讨论】:

  • 太棒了!!迟到总比不到好!这正是我想要的……我应该准确地说出我的要求。现在我已经开始使用它了,我意识到如果我可以在框架中指定我想要的像素坐标(相对于左上角),我会更喜欢。我正在使用 Paint/GIMP,我将光标放在要保留的部分的左上角并记录它报告的位置。然后我对右下角做同样的事情。理想情况下,我可以直接使用这些数字,而不是使用它们为您的出色脚本生成输入。要求修改是不是太过分了? :D
  • 哦,是否可以编写一个脚本,为多个文件重复运行该脚本?类似于: (script-fu-wirebear-edger "C:\\1.png" "C:\\1e.png" 1 2 3 4) (script-fu-wirebear-edger "C:\\2.png " "C:\\2e.png" 1 2 3 4) (script-fu-wirebear-edger "C:\\3.png" "C:\\3e.png" 1 2 3 4)
  • @Stainsor 我在上面添加了一个 Batch Edger 脚本,它允许您在一个充满图像的文件夹上运行该脚本。至于您关于指定像素坐标的第一个请求,我不确定我是否完全理解您在寻找什么。
【解决方案2】:

Gimp 不是适合这项工作的工具。安装 imagemagick 或 graphicsmagick。它包含convert 二进制文件。

请看这里:http://www.imagemagick.org/Usage/crop/#crop_repage

【讨论】:

  • 谢谢。看起来它可以完成这项工作。
  • 我尝试查找您链接到的 -crop 选项以外的其他内容,因为我想指定要从每一侧剃掉的像素数,而不是剩余图像的大小。但看起来-crop毕竟是最好的选择:convert test.png -crop 300x200+232+93 +repage test_crop.png ...无论如何我只有几个不同的图像尺寸。再次感谢!
  • 你想要的是剃须和砍命令,在同一页上:imagemagick.org/Usage/crop/#shave | imagemagick.org/Usage/crop/#chop [不幸的是 Imagemagick 文档组织得不是很好]
猜你喜欢
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-05
  • 2021-05-28
  • 1970-01-01
  • 2020-12-15
  • 1970-01-01
相关资源
最近更新 更多