linux - 如何使用 GIMP 编写自定义自动裁剪脚本?

标签 linux scripting image-processing crop gimp

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

我可以使用 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

最佳答案

边缘脚本

我已经编写了一个自定义 GIMP 脚本,它完全可以满足您的要求。只需将以下内容粘贴到一个文本文档中,并以 .scm 扩展名将其保存在您的 GIMP 脚本文件夹中(该路径可以在 Edit > Preferences > Folders > 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 > Script-Fu > 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 脚本,您可以将以下脚本与上述脚本结合使用(您的脚本文件夹中都需要):

(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 > Script-Fu > Console)中运行它 - 无论操作系统如何:

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

关于linux - 如何使用 GIMP 编写自定义自动裁剪脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5811378/

相关文章:

Linux 命名管道 - MKFIFO 查询

linux - 寻找丢失的共享库

linux - 有没有适用于 Linux 的 WebRTC 库?

python - 在opencv中调整图像大小

html - 将相机中的图像捕获到表单或 html5 Canvas 中

python - View 中缺少 "pass"

bash - #! 脚本可以用作解释器吗?散列线?

shell脚本: not able to assign integer to a variable

linux - 获取执行脚本的目录名称

math - 理解小波理论的先决条件