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

前端之家收集整理的这篇文章主要介绍了linux – 如何使用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)))

所有照片的整体尺寸都不相同.但它们在我想要裁剪的顶部,左侧,右侧和底部都具有相同数量的像素.因此,假设图像具有像素尺寸宽度和高度,我想从顶部移除t_junk像素,从底部移除b_junk像素,从左侧移除l_junk像素,从右移除r_junk像素.所以我希望新的图像尺寸为宽度 – l_junk – r_junk和height – t_junk – b_junk.

最佳答案
Edger Script

我编写了一个自定义的GIMP脚本,它完全符合您的要求.只需将以下内容粘贴到文本文档中,然后将其以.scm扩展名保存在GIMP脚本文件夹中(可以在编辑>首选项>文件夹>脚本中找到/创建路径):

(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控制台(过滤器> Script-Fu>控制台)中运行它 – 无论这样的操作系统如何:

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

批处理Edger脚本

为了在多个映像上运行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控制台(过滤器> Script-Fu>控制台)中运行它 – 无论这样的操作系统如何:

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

猜你在找的Linux相关文章