7

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

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

    (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_junkand height - t_junk - b_junk

4

2 回答 2

9

边缘脚本

我编写了一个自定义 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 脚本,您可以将以下脚本与上述脚本结合使用(您的 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 > Script-Fu > Console) 中运行它——不管操作系统是这样的:

(script-fu-wirebear-batch-edger "C:\\Users\\You\\Desktop\\Images\\*.png"
"_edged" 10 30 25 5)
于 2011-08-09T19:33:10.233 回答
4

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

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

于 2011-04-27T23:02:36.463 回答